Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

“Library Routine Called Out Of Sequence” sqlite3_prepare_v2(CREATE TABLE)

Tags:

sqlite

Do you know why do I get “Library Routine Called Out Of Sequence” when I call sqlite3_prepare_v2(CREATE TABLE) on an empty database?

I create an empty database and then open it. Later I save all information that has to be written to the database in RAM (I need to save that information in RAM and flush it to permanent storage at the end of execution), but I get this error message when I call sqlite3_prepare_v2(CREATE TABLE). It returns “Library Routine Called Out Of Sequence” as an error message.

I do open my database correctly and (I thought that it can be a problem so I did close() my DB and then open() right before calling sqlite3_prepare_v2(CREATE TABLE)). I thought it could be because of thread concurrency, but using critical section didn't help too.

like image 832
user1079655 Avatar asked Dec 04 '11 02:12

user1079655


1 Answers

This is what the documentation says about causes for your error:

  1. Calling any API routine with an sqlite3* pointer that was not obtained from sqlite3_open() or sqlite3_open16() or which has already been closed by sqlite3_close().
  2. Trying to use the same database connection at the same instant in time from two or more threads.
  3. Calling sqlite3_step() with a sqlite3_stmt* statement pointer that was not obtained from sqlite3_prepare() or sqlite3_prepare16() or that has already been destroyed by sqlite3_finalize().
  4. Trying to bind values to a statement (using sqlite3_bind_...()) while that statement is running.

You mentioned trying a critical section, so I guess we can rule out #2. Your error is a result of calling sqlite3_prepare_v2(...), not sqlite3_step() or or sqlite3_bind(), so I guess that only leaves #1? Can you double check that your db pointer is good? Trace it back to the sqlite3_open() that returned it and make sure nothing closed it before your prepare is called?

This works for me:

#include <stdio.h>
#include <sqlite3.h>

int main(int argc, char **argv){

    sqlite3 *db;
    int rc;

    char *db_name= ":memory:";

    rc = sqlite3_open(db_name, &db);

    if (rc != SQLITE_OK) {
        fprintf(stderr, "failed to open in memory database: %s\n", 
                sqlite3_errmsg(db));
        sqlite3_close(db);
        return(1);
    }

    const char *create_sql = "CREATE TABLE foo(bar TEXT)";
    sqlite3_stmt *statement;

    rc = sqlite3_prepare_v2(db, create_sql, -1, &statement, NULL);

    if (rc != SQLITE_OK) {
        fprintf(stderr, "failed to prepare statement: %s\n",
                sqlite3_errmsg(db));
        sqlite3_close(db);
        return(1);
    }

    rc = sqlite3_step(statement);

    if (rc == SQLITE_ERROR) {
        fprintf(stderr, 
                "failed to execute statement: %s\n", 
                sqlite3_errmsg(db));
    }

    sqlite3_close(db);

}
like image 56
Mark Johnson Avatar answered Oct 06 '22 12:10

Mark Johnson