【已解决】vs2015下c++对sqlite的操作
#include <iostream>
#include <sqlite3.h>
int callback(void* NotUsed, int argc, char** argv, char** azColName) {
NotUsed = 0;
for (int i = 0; i < argc; i++) {
std::cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << std::endl;
}
std::cout << std::endl;
return 0;
}
int main(int argc, char* argv[]) {
sqlite3* db;
char* zErrMsg = 0;
int rc;
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " DATABASE SQL-STATEMENT" << std::endl;
return(1);
}
rc = sqlite3_open(argv[1], &db);
if (rc) {
std::cerr << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return(1);
}
rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
std::cerr << "SQL error: " << zErrMsg << std::endl;
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
return 0;
}
这段代码展示了如何在Visual Studio 2015中使用C++和SQLite3库打开一个SQLite数据库,执行一个SQL命令,并处理返回的结果。代码使用了sqlite3\_open函数来打开数据库,sqlite3\_exec来执行SQL语句,并通过callback函数来处理查询结果。如果操作过程中出现错误,它会输出错误信息。
评论已关闭