Thanks for your answer.
Actually, I tried to have resets there although I think that finalize is as
good in releasing the locks on tables. It didn't help.
I do it with prepare and step because this script is a demo of a bug I have
in my code, where I use all over a certain function that prepare and step.
It is true that here I could use also exec.
However, if I change the script to have the first two create statements run
by sqlite3_exec, and change the last statement to a select (to justify the
use of prepare) I still get the "SQL logic error or missing database". So I
suspect that the problem is because I use two open connections to the
database (again, this is a demo of the bug which happen in much bigger
application where opening two connections to the same database make sense),
and somehow when I create a second table with the second connection it
locks/invalidate or whatever the first connection and I get this error. I
even checked the code with valgrind, but got nothing...
Any help will be appreciated.
Ran
On 5/23/06, Jay Sprenkle <[EMAIL PROTECTED]> wrote:
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?