Select and Update
-
Hi! I've to read each name in a field from a table and do some string mangling and update each name into another field. I've to query the database from C++. I'm using SQLite. I used the following code to do this:
SQLManager SQLdb;
SQLdb.init("../../data/config/playerDatabase.db");
array<stringc> ctyLst = SQLdb.Query("Select DISTINCT Team from Test ORDER BY Team DESC");
array<stringc> playerLst;
core::stringc queryStr,updateTest;
for(u32 i = 0; i<ctyLst.size();i++)
{
queryStr = L"Select Name From Test WHERE Team ='";
queryStr += ctyLst[i].trim();
queryStr += L"'";// queryStr += L" ORDER BY RANDOM() LIMIT 11";
array<stringc> playerTmpLst;
playerTmpLst = SQLdb.Query(queryStr);
for(u32 j = 0; j < playerTmpLst.size(); j++)
{
playerLst.push_back(playerTmpLst[j].c_str());
}
}I'm doing string mangiling an updating as follows:
for(int j = 0; j<playerLst.size(); j++)
{
updateTest = L"UPDATE Test SET MangledName ='";
unsigned int t = time(NULL);
srand(t);
playerLst[j] = playerLst[j].trim();
int index = playerLst[j].findLast(' ');
stringc subStr1 = playerLst[j].subString(0,index);
stringc subStr2 = playerLst[j].subString(index+1,15);
subStr1.append(" ",1);
subStr1.append(swapChars(subStr2).c_str(),subStr2.size());
updateTest += subStr1.trim();
updateTest += L"'";
SQLdb.Query(updateTest);
Sleep(10);
cout<<subStr1.c_str()<<endl;
t++;
}While printing the result to a file, it prints correctly. But only the last name in the array is updated to all the records in the field. How to Select a single name at a time and do the string mangling and update the name to the database?
-
Hi! I've to read each name in a field from a table and do some string mangling and update each name into another field. I've to query the database from C++. I'm using SQLite. I used the following code to do this:
SQLManager SQLdb;
SQLdb.init("../../data/config/playerDatabase.db");
array<stringc> ctyLst = SQLdb.Query("Select DISTINCT Team from Test ORDER BY Team DESC");
array<stringc> playerLst;
core::stringc queryStr,updateTest;
for(u32 i = 0; i<ctyLst.size();i++)
{
queryStr = L"Select Name From Test WHERE Team ='";
queryStr += ctyLst[i].trim();
queryStr += L"'";// queryStr += L" ORDER BY RANDOM() LIMIT 11";
array<stringc> playerTmpLst;
playerTmpLst = SQLdb.Query(queryStr);
for(u32 j = 0; j < playerTmpLst.size(); j++)
{
playerLst.push_back(playerTmpLst[j].c_str());
}
}I'm doing string mangiling an updating as follows:
for(int j = 0; j<playerLst.size(); j++)
{
updateTest = L"UPDATE Test SET MangledName ='";
unsigned int t = time(NULL);
srand(t);
playerLst[j] = playerLst[j].trim();
int index = playerLst[j].findLast(' ');
stringc subStr1 = playerLst[j].subString(0,index);
stringc subStr2 = playerLst[j].subString(index+1,15);
subStr1.append(" ",1);
subStr1.append(swapChars(subStr2).c_str(),subStr2.size());
updateTest += subStr1.trim();
updateTest += L"'";
SQLdb.Query(updateTest);
Sleep(10);
cout<<subStr1.c_str()<<endl;
t++;
}While printing the result to a file, it prints correctly. But only the last name in the array is updated to all the records in the field. How to Select a single name at a time and do the string mangling and update the name to the database?
One normally uses a WHERE clause to specify which record(s) get updated. Remarks: - what is the purpose of Sleep(10)? - what would happen if a player's name contained a quote? - why don't you use SqlParameters instead of string concatenation? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.