Search code examples
c++sqlsqliteparsingrecv

C++ SQLite return some values?


I am coding a serverside program that uses SQLite. When a client logs in, I want to get its informations from a Userinfo table.

Its columns are

  • Userno(int),
  • money(int),
  • bankmoney(int),
  • gender(string),
  • name,
  • surname

I want to write these informations to a user structure

struct userstruct
{
  int userno;
  int money;
  int bankmoney
  ....
}

When a user logs in I want to create a new struct and set infos of user from this table via SQLite.


Solution

  • The following:

    static int callback(void *data, int argc, char **argv, char **azColName)
    
    • void *data - can be used to give pointer to an object - aka this, or anything else, can be null
    • int argc - column count - number of selected records
    • char **argv - ARRAY of column values.
    • char **azColName - ARRAY of column names

    btw: better use prepare - bind - step, like the following sample, this way it works without a callback:

    sqlite3_prepare_v2(db, "select distinct name, age from demo where age > ? order by 2,1;", -1, &stmt, NULL);
    
    sqlite3_bind_int(stmt, 1, 16);                                                                  /* 1 */
    
    while ( (rc = sqlite3_step(stmt)) == SQLITE_ROW) {                                              /* 2 */
        printf("%s is %d years old\n", sqlite3_column_text(stmt, 0), sqlite3_column_int(stmt, 1));  /* 3 */
    }