On 5/23/06, Ran <[EMAIL PROTECTED]> wrote:
Hi all,


  rc = sqlite3_prepare(db1,                // Database handle
                       "create table bla(a int,b int)",
                       -1,                 // Length of the statement
                       &pStmt1,       // OUT: Statement handle
                       0);                 // OUT: Pointer to unused portion
                                           // of the statement

  rc = sqlite3_step(pStmt1);
  if (rc != SQLITE_DONE) { // if we failed, we show it.
    printf("Failed to step statement: %s\n", sqlite3_errmsg(db1));
  }
  rc = sqlite3_finalize(pStmt1);
  sqlite3_exec(db1, "commit", 0, 0, 0); // here we commit the transaction.

You forgot the reset here:
int sqlite3_reset(sqlite3_stmt *pStmt);
http://sqlite.org/capi3ref.html#sqlite3_reset

Why are you preparing this statement?
Just sqlite3_exec() it.


  // now we suppose to have inside the database the table bla.

  // here we, optionally, create another connection to the same database,
  // and then create other table in a transaction.
  if (argc > 1) {
    rc = sqlite3_open("bug.db", &db2); // create the second connection.
    if (rc) {
      printf("Cannot open database again: %s\n", sqlite3_errmsg(db2));
      exit(1);
    }
    else {
      printf("Opened the database.\n");
    }

You still have a valid handle to the open database. Why create a second one?



    // create table foo
    sqlite3_exec(db2, "begin", 0, 0, 0); // start the transaction.
    rc = sqlite3_prepare(db2,                // Database handle
                         "create table foo(c int,d int)",
                         -1,                 // Length of the statement
                         &pStmt2,            // OUT: Statement handle
                         0);                 // OUT: Pointer to unused
portion
                                             // of the statement

    rc = sqlite3_step(pStmt2);
    if (rc != SQLITE_DONE) { // if we failed, we show it.
      printf("Failed to step statement: %s\n", sqlite3_errmsg(db2));
    }
    rc = sqlite3_finalize(pStmt2);
    sqlite3_exec(db2, "commit", 0, 0, 0);
  }

  // delete from table bla using the first connection.
  sqlite3_exec(db1, "begin", 0, 0, 0);
  rc = sqlite3_prepare(db1,                // Database handle
                       "delete from bla",
                       -1,                 // Length of the statement
                       &pStmt3,       // OUT: Statement handle
                       0);                 // OUT: Pointer to unused portion
                                           // of the statement

  rc = sqlite3_step(pStmt3);
  if (rc != SQLITE_DONE) { // if we failed, we log it.
    printf("Failed to step statement: %s\n", sqlite3_errmsg(db1));
  }
  else {
    printf("deleted all from bla successfully\n");
  }
  rc = sqlite3_finalize(pStmt3);
  sqlite3_exec(db1, "commit", 0, 0, 0);
}

Again, why prepare something that returns no results and will
not be used more than once?

Reply via email to