我正在使用 Visual Studio 2015 ,基本上我想从 sqlite 数据库检索一些数据并将其存储在字符串中。 下面的代码只是在屏幕上显示数据,有什么方法可以将其存储在字符串中。 PS:我已经通过命令提示符创建了表..
int main()
{
char *zErrMsg = 0;
sqlite3 *db;
int rc;
rc = sqlite3_open("testingvisual.db", &db);
//string sql;
char *data;
const char *sql;
sql = "INSERT INTO TESTDATA VALUES (1,'test','test2');";
sqlite3_exec(db, sql, callback, 0, &zErrMsg);
sql = "SELECT * FROM TESTDATA WHERE id=1;";
sqlite3_exec(db, sql, callback, 0, &zErrMsg);
}
请您参考如下方法:
当您希望检索数据(即处理 SELECT 语句)时,请使用 API sqlite3_prepare_v2 和 sqlite3_step。正如您所发现的,sqlite3_exec 是处理 INSERT、UPDATE、DELETE 和数据定义函数的正确选择。下面是对 SELECT 查询的粗略处理,以帮助您入门:
sqlite3_stmt* t_statement;
sql="SELECT * FROM TESTDATA WHERE id=1;";
size_t t_len = strlen(sql);
int rc = sqlite3_prepare_v2(db, sql, t_len, &t_statement, &zErrMsg);
if (rc == SQLITE_OK)
{
rc = sqlite3_step(t_statement);
if (rc == SQLITE_OK)
{
// iterate over the columns to get the data
const char* t_value =
sqlite3_column_text(t_statement, [column_number]);
}
}
else
{
// zErrMsg may have an error message. If not, the database object
// may have one. Either way, you can find out what went wrong.
const char* db_error_msg = sqlite3_errmsg(db);
}
我将浏览器书签设置为 https://www.sqlite.org/c3ref/funclist.html 。 FWIW,我构建了一个类来管理我的 SQLite 交互,以便我的代码可以对 SQLite API 函数进行友好的调用(例如 ExecSQL()、SetIsActive()、FetchNext()、GetFieldByName() 等)您可能也想做类似的事情。






