Hi all, Could someone help me with the script below? I get an "SQL logic error or missing database" and cannot find what I do wrong. I use sqlite 3.3.4 on Linux.
What I do there is: 1. Open connection to a new database. 2. Create table bla in a transaction. 3. Open another connection to the database. 4. Create table foo in a transaction using the second connection. 5. Try to delete from the table bla using the first connection. ==> this gives "SQL logic error or missing database". Here is how it runs (without arguments, the second connection is not opened, and the table foo is not created - and this runs OK): ./bug Opened the database. deleted all from bla successfully ./bug 1 Opened the database. Opened the database. Failed to step statement: SQL logic error or missing database Here is the script: // compile with: gcc -g bug.cpp -lsqlite3 -o bug #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sqlite3.h> int main(int argc, char** argv) { int rc; sqlite3* db1; sqlite3* db2; sqlite3_stmt *pStmt1; sqlite3_stmt *pStmt2; sqlite3_stmt *pStmt3; unlink("bug.db"); // for the test, we make sure we have a new database. // create first connection to the database: db1. rc = sqlite3_open("bug.db", &db1); if (rc) { printf("Cannot open database: %s\n", sqlite3_errmsg(db1)); exit(1); } printf("Opened the database.\n"); // create table bla using the first connection db1, inside a transaction. sqlite3_exec(db1, "begin", 0, 0, 0); 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. // 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"); } // 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); }