hashDefine
hashDefine

Reputation: 1491

SQLite: "Prepare Statement" not working

I am trying to use "prepare statement" functionality from here in C and VS2012 to save bulk data. here is my code:

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);
}

but it is not working. whenever I check the database in "SQLite administrator", I find nothing except the table itself, i.e.: No Data.

why is this happening ?

Upvotes: 1

Views: 4275

Answers (1)

McLan
McLan

Reputation: 2678

try this:

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);
}

Upvotes: 2

Related Questions