1

我正在尝试在 C 和 VS2012 中使用此处的“准备语句”功能来保存批量数据。这是我的代码:

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 管理员”中检查数据库时,除了表本身,我什么也找不到,即:没有数据。

为什么会这样?

4

1 回答 1

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);
}
于 2013-09-05T11:00:59.330 回答