尝试按用户指定的值更新表。但是这些值没有得到更新。
cout<<"\nEnter Ac No"<<endl;
cin>>ac;
cout<<"\nEnter Amount"<<endl;
cin>>amt;
/* Create merged SQL statement */
sql = "UPDATE RECORDS set BAL = '%d' where ACCOUNT_NO = '%d'",amt, ac;
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
如果我用某个整数值而不是占位符替换 BAL 和 ACCOUNT_NO,那么它工作正常。
最佳答案
您的 sql 字符串未正确创建。
如果你期待这段代码
sql = "UPDATE RECORDS set BAL = '%d' where ACCOUNT_NO = '%d'",amt, ac;
以导致
“更新记录设置 BAL = '1' 其中 ACCOUNT_NO = '2'”
在哪里
amt= 1 和 ac = 2 那么你需要使用这样的字符串格式化调用。
// the buffer where your sql statement will live
char sql[1024];
// write the SQL statment with values into the buffer
_snprintf(sql,sizeof(sql)-1, "UPDATE RECORDS set BAL = '%d' where ACCOUNT_NO = '%d'",amt, ac);
buff[sizeof(sql)-1]='\0';
在您的特定平台上 _snprintf(...)
可能是 snprintf(..)
或另一个类似命名的函数。此外,您的编译器可能会警告缓冲区操作安全漏洞。根据您的需要选择合适的替代品
关于c++ - SQLite 更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42795240/