Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select data from an sqlite3 database into variables using sqlite3_exec?

There are a million examples on the interweb detailing the use of sqlite3_exec to select rows from a table and printing them using this callback function:

static int callback(void *NotUsed, int argc, char **argv, char **azColName){
  int i;
  for(i=0; i<argc; i++){
    printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  }
  printf("\n");
  return 0;
}

This is the code that implements callback:

[...]
rc = sqlite3_exec(db, sql, callback, &nrecs, &zErrMsg);
[...]

Now, I would assume that using the fetched data for something other than just printing would be a rather standard scenario, but for the life of me I cannot find out how this is supposed to be accomplished.

What I wish to do is fetch one row (argc will be 1) containing two columns and have these two values (argv[0] and argv[1]) accessible from where sqlite3_exec was executed.

I suppose this has something to do with the void *NotUsed. (What in the world is this a pointer to and why do all examples insist on not using it?)

If someone could help me with this i would be ever so grateful. If you could also explain to me why this seemingly trivial task has been made so complicated you will have my undying love. (The fetched data is handled in a static function called from another function. This effectively kills the purpose of using an object oriented language, right?)

like image 522
Martin Avatar asked Jun 24 '15 14:06

Martin


People also ask

How do I get data from sqlite3?

First, establish a connection to the SQLite database by creating a Connection object. Next, create a Cursor object using the cursor method of the Connection object. Then, execute a SELECT statement. After that, call the fetchall() method of the cursor object to fetch the data.

Can you use variables in SQLite?

SQLite doesn't support native variable syntax, but you can achieve virtually the same using an in-memory temp table.

How can I get all data from a table in SQLite?

If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.

How do I access SQLite data?

Open a command prompt (cmd.exe) and 'cd' to the folder location of the SQL_SAFI. sqlite database file. run the command 'sqlite3' This should open the SQLite shell and present a screen similar to that below.


1 Answers

The 'key' is the fourth parameter of sqlite3_exec.

Usually the scenario is like this: You have this struct you weant to fill in with sqlite query:

struct xyz_t
{
   int a ;
   int b ;
} ;

then you call sqlite3_exec like this:

//fill in query text
xyz_t xyz; // struct you want to fill
sqlite3_exec(db, sql, callback, &xyz, &zErrMsg);

And the callback does this:

static int callback(void *veryUsed, int argc, char **argv, char **azColName){
  int i;
  xyz_t *xyz = (xyz_t *)veryUsed ;
  for(i=0; i<argc; i++){
    if (strcmp(azColName[i], "a") == 0)
       xyz->a = atoi(argv[1]) ;
    else if ...
  }
  return 0;
}

You can pass even a std::vector and push_back a new xyz_t on every call. Not nice for a C++ programmer but this is a mixed C/C++ world...

like image 198
marom Avatar answered Oct 11 '22 20:10

marom