简体   繁体   中英

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.

You bind twice item 2 of the statement and? is missing for item 3.

The size of the zSql statement is 69 characters. It is is better to us -1 for length as by default it will be null-terminated string and size is automatically computed:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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