我正在尝试使用C和VS2012中here的“prepare statement”功能来保存批量数据。这是我的代码:
char *sql;
char query[1000];
char buffer[100] = "INSERT INTO Table1 VALUES (?NNN1, ?2, ?3)";
sqlite3_int64 i;
sprintf_s(query , "CREATE TABLE IF NOT EXISTS Table1("
"ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
"Name CHAR NULL , "
"Title CHAR NULL ); ");
sql = query ;
db_execute_sql(db,sql,1); // create the table "Table1"
fprintf(stdout,"Table1");
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, buffer,strlen(buffer), &stmt,NULL);
for(i = 0; i < 3; i++){
sqlite3_bind_int64(stmt,1,NULL);
sqlite3_bind_text(stmt,2,"Two",-1,SQLITE_TRANSIENT);
sqlite3_bind_text(stmt,3,"Three",-1,SQLITE_TRANSIENT);
if(sqlite3_step(stmt) != SQLITE_DONE){
printf("\n\nPrepare statement failed!"); // it always comes here
}
sqlite3_reset(stmt);
}
但它不起作用。每当我在“SQLite管理员”中检查数据库时,除了表本身,我什么也找不到,即:没有数据。
为什么会这样?
答案 0 :(得分:2)
试试这个:
char *sql;
char query[1000];
char buffer[100] = "INSERT INTO Table1 VALUES (?1, ?2, ?3)";
sqlite3_int64 i;
sprintf_s(query , "CREATE TABLE IF NOT EXISTS Table1("
"ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
"Name CHAR NULL , "
"Title CHAR NULL ); ");
sql = query ;
db_execute_sql(db,sql,1); // create the table "Table1"
fprintf(stdout,"Table1");
sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, buffer,strlen(buffer), &stmt,NULL);
for(i = 0; i < 3; i++){
// remove the first insert since it is autoincremental
sqlite3_bind_text(stmt,2,"Two",-1,SQLITE_TRANSIENT);
sqlite3_bind_text(stmt,3,"Three",-1,SQLITE_TRANSIENT);
if(sqlite3_step(stmt) != SQLITE_DONE){
printf("\n\nPrepare statement failed!"); //
}
sqlite3_reset(stmt);
}