简体   繁体   English

Sqlite3 在数据库中插入数据在 c 中不起作用

[英]Sqlite3 insert data in database does not work in c

   sqlite3_stmt *stmt;
   sqlite3_prepare_v2(db, "INSERT INTO links(NAME, LINK, SIZE, STARRED)  VALUES ('?' , '?', ? , 0);", 41, &stmt, NULL);
   if(stmt != NULL) {
      sqlite3_bind_text(stmt, 1, name, 0, SQLITE_TRANSIENT);
      sqlite3_bind_text(stmt, 2, link, 0, SQLITE_TRANSIENT);
      sqlite3_bind_int(stmt, 3, size);
      sqlite3_step(stmt);
      sqlite3_finalize(stmt);
   }else{
     printf("%sError during insertion in the database%s\n", RED, RST);
   }

  sqlite3_close(db);

I always get on the output Error during insertion in the database but can't understand why, maybe something related to sqlite3_prepare_v2 but doesn't know what, I tried to execute the query 'manually' with random data and it works. Error during insertion in the database但不明白为什么,可能与sqlite3_prepare_v2相关但不知道是什么,我尝试使用随机数据“手动”执行查询,它可以工作。

You bind twice item 2 of the statement and?您将语句的第 2 项绑定两次? is missing for item 3.缺少第 3 项。

The size of the zSql statement is 69 characters. zSql 语句的大小为 69 个字符。 It is is better to us -1 for length as by default it will be null-terminated string and size is automatically computed:长度为 -1 对我们来说更好,因为默认情况下它将是以空字符结尾的字符串,并且会自动计算大小:

sqlite3_prepare_v2(db, "INSERT INTO links(NAME, LINK, SIZE, STARRED)  VALUES (?, ?, ? , 0);", -1, &stmt, NULL);

Do not forget the 3rd '?'不要忘记第三个'? in the statement for the 3rd argument.在第三个论点的陈述中。 Edit: write it as?编辑:写成? in the statement在声明中

Just solved by changing the third parameter of sqlite3_prepare_v2() that is the maximum length of zSql in bytes, and 47 is to small.刚刚通过更改sqlite3_prepare_v2()的第三个参数即zSql的最大长度(以字节为单位)来解决,47太小了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM