可在不SQLITE prepare INSERT语句,C API [英] Can not prepare INSERT statement in SQLITE, C API
问题描述
/* DATABASE INIT */
ret = sqlite3_open_v2(dbfile, &DB, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (SQLITE_OK != ret) {
printf("Could not open database.\n");
exit(1);
}
const char* zSql = "INSERT INTO abc VALUES (?)";
ret = sqlite3_prepare_v2(sites[DB, zSql, strlen(zSql), &s, NULL);
if (SQLITE_OK != ret) {
printf("Could not compile statement '%s', got %d.\n", zSql, ret);
exit(1);
}
好了,你去那里。怎么了? sqlite3_ prepare_v2总是失败。
Well, there you go. What's wrong? sqlite3_prepare_v2 always fails.
更新:
我试过到目前为止的建议,但没有雪茄。
我创造了我能想到的最简单的情况下,用一个模式:
Update: I tried the suggestions so far, but no cigar. I created the simplest case I could think of, with a schema:
CREATE TABLE abc(a INTEGER);
而C code作为以上,仍然不能正常工作,得到无法编译陈述
得到了回报code 26这显然意味着 SQLITE_NOTADB
。因此,似乎我的数据库文件是不是... ...一个数据库文件。奇怪,我将不得不考虑这一点。
And the C code as above, still does not work, gets 'Could not compile statement'
got return code 26 which apparently means SQLITE_NOTADB
. So it seems my database file is not... a database file. Strange, I will have to look into this.
SQLite的CLI接受该文件,并能显示模式,如果我用使用.dump命令。
The sqlite CLI accepts the file and can show the schema if I use the ".dump" command.
我是从data.db到data.sqlite更改的数据库文件名。现在我得到的1回报code,SQLITE_ERROR代替。
I changed the database file name from "data.db" to "data.sqlite". Now I get a return code of 1, SQLITE_ERROR, instead.
的更新:的
我访问了错误的文件,AKA 用户错误。
接受,在我的未经编辑的提问时指出语法错误的答案之一。
Update: I was accessing the wrong file, AKA user error. Accepting one of the answers that pointed out a syntax error in my unedited question.
推荐答案
我想你应该尝试
const char* zSql =
"INSERT INTO abc (moderation_status, phonenumber, email) VALUES(?,?,?)";
假设 moderation_status
PHONENUMBER
和电子邮件
是名在表中的字段。
assuming moderation_status
phonenumber
and email
are the names of the fields in your table.
或
const char* zSql = "INSERT INTO abc VALUES(?,?,?)";
的?
是这里的参数将被插入的占位符。
The ?
is the placeholder for where the arguments will be inserted.
这篇关于可在不SQLITE prepare INSERT语句,C API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!