首页 > 解决方案 > insert blob into sqlite 3 with C

问题描述

So I'm trying to save onto a database a blob but it doesn't insert anything. I checked the return errors and I always get "error 0: not an error"

I'm using the sqlite3.org example for blob management: https://www.sqlite.org/cvstrac/wiki?p=BlobExample

Anyway here is the exact code, maybe I'm forgetting something.

int Database::insertToDatabase(
const int zKey,              /*Id of row */
const unsigned char *zBlob,    /* Pointer to blob of data */
int nBlob,                     /* Length of data pointed to by zBlob */
){
char zSql[kMaxSqlFunctionSize];
snprintf(zSql, kMaxSqlFunctionSize, "INSERT INTO RECTS(ID,DATA)VALUES(%d,?)",zKey);
sqlite3_stmt *pStmt;
int rc;

do {
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, NULL);
if( rc!=SQLITE_OK ){
  return rc;
}
sqlite3_bind_blob(pStmt, 1, zBlob, nBlob, SQLITE_STATIC);
rc = sqlite3_step(pStmt);
assert( rc!=SQLITE_ROW );

rc = sqlite3_finalize(pStmt);

} while( rc==SQLITE_SCHEMA );

return rc;
}

I use sqlite_open_v2 to open the database and the tables are created correctly (checked with sqlite studio), but idk why I can't get this insert to work. (the data I'm trying to insert isn't more than 1k). Thanks in advance.

标签: csqliteinsertblob

解决方案


推荐阅读