user2984715
user2984715

Reputation: 133

Table creation in sqlite3 isn't working

I have a problem in the creation of a one table in sqlite3. Basically, the code (language c) that I use in the creation is the following:

do{
    sprintf(buffer, "CREATE TABLE new_tab AS SELECT * FROM fileslog WHERE file_owner='%s' AND  state='%s';", file_owner, state);

    rc = sqlite3_prepare(db, buffer, -1, &result, NULL);

 }while((rc == SQLITE_BUSY) || (rc == SQLITE_LOCKED));

My problem is that no any table is created when I execute this code. I have printed the rc variable to see the possible errors, but its value is 0 (SQLITE_OK). I don't know that it's happening neither where is the error.

Upvotes: 0

Views: 87

Answers (2)

laalto
laalto

Reputation: 152827

sqlite_prepare_v2() followed by sqlite3_step()s and `sqlite3_finalizer() as suggested by Lasse V. Karlsen is one way to run the SQL.

sqlite3_exec() is a simpler way for CREATE TABLE and other non-SELECT queries where you don't need to get result rows. As a side effect, you can't use variable binding (that can be useful for e.g. UPDATE and DELETE queries).

Upvotes: 1

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391396

You are only preparing the SQL statement for execution.

To actually execute it, call sqlite3_step.

The steps involved according to the the SQL Statement Object documentation are:

  1. Create the object using sqlite3_prepare_v2() or a related function.
  2. Bind values to host parameters using the sqlite3_bind_*() interfaces.
  3. Run the SQL by calling sqlite3_step() one or more times.
  4. Reset the statement using sqlite3_reset() then go back to step 2. Do this zero or more times.
  5. Destroy the object using sqlite3_finalize().

(above list and links lifted from the official documentation.)

Upvotes: 3

Related Questions