我正在尝试使用SQLite数据库的user_version功能。我无法实际设置user_version,我不知道为什么。我已经尝试了各种执行查询来更新user_version的方法,并且已经在线进行了大量搜索,现在我已经全部丢失了。
这是我的查询字符串。
const std::string kSetUserVersion = "PRAGMA user_version = 1;";
这是我尝试设置user_version的地方,但是我的结果仍为0。
// Set the user version and check it is correct.
sqlite3_exec(dbConnection->db_handle, kSetUserVersion.c_str(), nullptr, nullptr, &db_err); // <-- this is not working!
long currentVersion = sqlite3_exec(dbConnection->db_handle, kGetUserVersion.c_str(), nullptr, nullptr, &db_err);
// If the PRAGMA statement fails, no error returns and it fails silently, so having to add check to see if it has worked.
if (currentVersion != 1) // Setting the user_version has failed.
{
throw DatabaseAccessException(sqlite3_errmsg(dbConnection->db_handle));
}
非常感谢任何帮助。
答案 0 :(得分:2)
sqlite3_exec()
返回值是状态/错误代码,而不是查询中的值。
要获取查询结果值,有两个主要选项:
对sqlite3_prepare_v2()
使用sqlite_step()
,SQLITE_ROW
并使用例如sqlite3_column_int()
访问数据sqlite3_finalize()
,使用sqlite3_exec()
完成准备好的查询。
提供一个回调函数作为{{1}}的第三个参数来捕获值。参见例如例如:Proper use of callback function of sqlite3 in C++