简体   繁体   中英

How do I store into a C++ variables using sqlite3?

I'm new in the world of C++.

I'm trying to store into a variable a value contained in a sqlite table that I've created but I don't know how to do (I research a lot befor asking here).

So, after I open the DB connection I execute this:

   char* sql = new char[4096];
   strcpy(sql, statement.c_str());

   /* Execute SQL statement */
   int rc = sqlite3_exec(DB, sql, callback, 0, &zErrMsg);

   if( rc != SQLITE_OK ){
      fprintf(stderr, "SQL ERROR: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
   } else {
      fprintf(stdout, "STATEMENT:\n\n%s\n\nEXECUTED SUCCESSFULLY!\n\n", statement.c_str());
   }

And I get this:

OPENED DATABASE SUCCESSFULLY
sent = 0

sent = 0

sent = 0

sent = 0

sent = 0

sent = 1

sent = 1

sent = 1

STATEMENT:

SELECT sent FROM Message;

EXECUTED SUCCESSFULLY!

What I want to do is to store the value contained in "sent" (the datatype in the db is boolean) in a int variables that I can manipulate to check some condition. Or maybe to store all the values into a int array.

How can I do? Please help me!

Thanks a lot!

EDIT: I'm using sqlite3 library.

And this is my 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;
}

callback is a function called for each row of the result set. You can assign the values to an array or vector in that function. https://www.sqlite.org/c3ref/exec.html

The 2nd argument to the sqlite3_exec() callback function is the number of columns in the result. The 3rd argument to the sqlite3_exec() callback is an array of pointers to strings obtained as if from sqlite3_column_text(), one for each column. If an element of a result row is NULL then the corresponding string pointer for the sqlite3_exec() callback is a NULL pointer. The 4th argument to the sqlite3_exec() callback is an array of pointers to strings where each entry represents the name of corresponding result column as obtained from sqlite3_column_name().

You need something like:

int callback(void *p, int size, char **column_text, char **column_name) {
    if (size == 0) return -1;
    auto &container = *static_cast<std::vector<std::string>*>(p);
    if (!column_text[0]) container.push_back("NULL");
    else container.push_back(column_text[0]);
    return 0;
}

and then you can store the values in your container with:

std::vector<std::string> container;
/* Execute SQL statement */
int rc = sqlite3_exec(DB, statement.c_str(), callback, &container, &zErrMsg);

if( rc != SQLITE_OK ){
   fprintf(stderr, "SQL ERROR: %s\n", zErrMsg);
   sqlite3_free(zErrMsg);
} else {
   fprintf(stdout, "STATEMENT:\n\n%s\n\nEXECUTED SUCCESSFULLY!\n\n", statement.c_str());
}

Don't use sqlite3_exec() for anything that needs to do anything with the results of a query, or anything that involves user-supplied values. Use a prepared statement .

Something like

sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(DB, statement.c_str(), statement.length(), &stmt, nullptr);
if (rc != SQLITE_OK) {
 // handle the error
}
// Loop through the results, a row at a time.
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
 int sent = sqlite3_column_int(stmt, 0);
 // etc.
}
// Free the statement when done.
sqlite3_finalize(stmt);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM