简体   繁体   English

如何使用 sqlite3 存储到 C++ 变量中?

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

I'm new in the world of C++.我是 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).我试图将一个包含在我创建的 sqlite 表中的值存储到一个变量中,但我不知道该怎么做(我在这里询问之前做了很多研究)。

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.我想要做的是将“sent”中包含的值(db 中的数据类型是布尔值)存储在一个 int 变量中,我可以操纵它来检查某些条件。 Or maybe to store all the values into a int array.或者可能将所有值存储到一个 int 数组中。

How can I do?我能怎么做? Please help me!请帮我!

Thanks a lot!非常感谢!

EDIT: I'm using sqlite3 library.编辑:我正在使用 sqlite3 库。

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. callback是为结果集的每一行callback的函数。 You can assign the values to an array or vector in that function.您可以将值分配给该函数中的数组或向量。 https://www.sqlite.org/c3ref/exec.html https://www.sqlite.org/c3ref/exec.html

The 2nd argument to the sqlite3_exec() callback function is the number of columns in the result. sqlite3_exec() 回调函数的第二个参数是结果中的列数。 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. sqlite3_exec() 回调的第三个参数是一个指向字符串的指针数组,就像从 sqlite3_column_text() 获得的字符串一样,每列一个。 If an element of a result row is NULL then the corresponding string pointer for the sqlite3_exec() callback is a NULL pointer.如果结果行的元素为 NULL,则 sqlite3_exec() 回调的相应字符串指针是 NULL 指针。 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(). sqlite3_exec() 回调的第四个参数是一个指向字符串的指针数组,其中每个条目表示从 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.不要将sqlite3_exec()用于任何需要对查询结果做任何事情的事情,或者任何涉及用户提供的值的事情。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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