Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Select and Update

Select and Update

Scheduled Pinned Locked Moved Database
databasec++asp-netsqlitedata-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    T RATHA KRISHNAN
    wrote on last edited by
    #1

    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?

    L 1 Reply Last reply
    0
    • T T RATHA KRISHNAN

      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?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups