不知道为啥 sqlite3 里面使用 "UPDATE" 更新数据,更新失败也不报错,所以就想绕过去。先查一下字段存在不存在,然后再update
cpp
std::string selectsql = "SELECT COUNT(*) FROM table_name WHERE some_condition";
char** dbResult;
char* errmsg = NULL; //用来存储错误信息字符串
int nRow = 0, nColumn = 0; // nRow 查找出的总行数,nColumn 存储列
int ret = sqlite3_get_table(db, selectsql.c_str(), &dbResult, &nRow, &nColumn, &errmsg);
if (SQLITE_OK != ret || nRow == 0)
{
sqlite3_free_table(dbResult);
// do_something
}
sqlite3_free_table(dbResult);
// do_something
std::string sql = "UPDATE table_name SET item1=? \
WHERE item2=? and item3=? and item4=?; ";
sqlite3_prepare_v2(db, sql.c_str(), -1, &statement, nullptr);
int index = 0;
QByteArray inByteArray
sqliteBind(statement, index, inByteArray.data(), inByteArray.length());
sqliteBind(statement, index, "...");
sqliteBind(statement, index, "...");
sqliteBind(statement, index, "...");