liubin liu wrote:
> I want to insert several pieces of data (include blob) into a table in a
> transaction by programing in C language , like:
> char *sql[n+2];
> 
> sql[0] = sqlite3_mprintf ("BEGIN;");
> 
> char *sql_f = "INSERT OR REPLACE INTO collstate VALUES (%d, %d, ?);";

Why don't you use parameters for all three values in the VALUES clause? Then 
you would only need one statement.

> for (i=1; i<n; i++)
>    ret = sqlite3_bind_blob (p_stmt, i, st_mydata[i].data, sizeof
> (st_mydata[i].data), SQLITE_STATIC);

The second parameter to sqlite3_bind_blob (in fact, all sqlite3_bind_* 
functions) is the index of a parameter within the statement. In your case, it 
should always be 1 (since you have only one parameter). Passing 'i' there 
doesn't make any sense.

So, in the end, you'll have somethng like this:

sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);

sqlite3_stmt* stmt;
const char* sql = "INSERT OR REPLACE INTO collstate VALUES (?, ?, ?);";
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);

for (int i = 0; i < n; ++i) {
  sqlite3_bind_int(stmt, 1, st_mydata[i].m1);
  sqlite3_bind_int(stmt, 2, st_mydata[i].m2);
  sqlite3_bind_blob(stmt, 3, st_mydata[i].data, sizeof(st_mydata[i].data), 
SQLITE_STATIC);

  sqlite3_step(stmt);
  sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);

sqlite3_exec(db, "END", NULL, NULL, NULL);

-- 
Igor Tandetnik

_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to