Jepessen
Jepessen

Reputation: 12445

trying to create a table gives a sql logic error

I'm trying to create a SQLite database. I create a new file and then I want to create this table:

CREATE TABLE 'session_properties'
(
    'user_name' TEXT NOT NULL, -- Name of the user.
    'user_surname' TEXT NOT NULL, -- Surname of the user.
    'description' TEXT NOT NULL, -- Description of the session.
    CONSTRAINT 'PK_user' PRIMARY KEY ('user_name','user_surname')
)
;

This is my code:

void checkErrorCode(int code){
  if (code != SQLITE_OK) {
    auto message = sqlite3_errstr(code);
    throw WSession::Exception(code, message);
  }
}

…

const char CreateDatabase_sql[] = {0x43,0x52,0x45,0x41,0x54,0x45,0x20,0x54,0x41,0x42,0x4c,0x45,0x20,0x27,0x73,0x65,0x73,0x73,0x69,0x6f,0x6e,0x5f,0x70,0x72,0x6f,0x70,0x65,0x72,0x74,0x69,0x65,0x73,0x27,0x0d,0x0a,0x28,0x0d,0x0a,0x09,0x27,0x75,0x73,0x65,0x72,0x5f,0x6e,0x61,0x6d,0x65,0x27,0x20,0x54,0x45,0x58,0x54,0x20,0x4e,0x4f,0x54,0x20,0x4e,0x55,0x4c,0x4c,0x2c,0x20,0x2d,0x2d,0x20,0x4e,0x61,0x6d,0x65,0x20,0x6f,0x66,0x20,0x74,0x68,0x65,0x20,0x75,0x73,0x65,0x72,0x2e,0x0d,0x0a,0x09,0x27,0x75,0x73,0x65,0x72,0x5f,0x73,0x75,0x72,0x6e,0x61,0x6d,0x65,0x27,0x20,0x54,0x45,0x58,0x54,0x20,0x4e,0x4f,0x54,0x20,0x4e,0x55,0x4c,0x4c,0x2c,0x20,0x2d,0x2d,0x20,0x53,0x75,0x72,0x6e,0x61,0x6d,0x65,0x20,0x6f,0x66,0x20,0x74,0x68,0x65,0x20,0x75,0x73,0x65,0x72,0x2e,0x0d,0x0a,0x09,0x27,0x64,0x65,0x73,0x63,0x72,0x69,0x70,0x74,0x69,0x6f,0x6e,0x27,0x20,0x54,0x45,0x58,0x54,0x20,0x4e,0x4f,0x54,0x20,0x4e,0x55,0x4c,0x4c,0x2c,0x20,0x2d,0x2d,0x20,0x44,0x65,0x73,0x63,0x72,0x69,0x70,0x74,0x69,0x6f,0x6e,0x20,0x6f,0x66,0x20,0x74,0x68,0x65,0x20,0x73,0x65,0x73,0x73,0x69,0x6f,0x6e,0x2e,0x0d,0x0a,0x09,0x43,0x4f,0x4e,0x53,0x54,0x52,0x41,0x49,0x4e,0x54,0x20,0x27,0x50,0x4b,0x5f,0x75,0x73,0x65,0x72,0x27,0x20,0x50,0x52,0x49,0x4d,0x41,0x52,0x59,0x20,0x4b,0x45,0x59,0x20,0x28,0x27,0x75,0x73,0x65,0x72,0x5f,0x6e,0x61,0x6d,0x65,0x27,0x2c,0x27,0x75,0x73,0x65,0x72,0x5f,0x73,0x75,0x72,0x6e,0x61,0x6d,0x65,0x27,0x29,0x0d,0x0a,0x29,0x0d,0x0a,0x3b,0x0d,0x0a,0x00};
// you can change the string with this one:
// const char CreateDatabase_sql[] = "CREATE TABLE 'session_properties' ('user_name' TEXT NOT NULL, 'user_surname' TEXT NOT NULL, 'description' TEXT NOT NULL, CONSTRAINT 'PK_user' PRIMARY KEY ('user_name','user_surname'));\0";
const unsigned CreateDatabase_sql_size = sizeof(CreateDatabase_sql);
sqlite3_stmt* statement;
sqlite3* m_database;
checkErrorCode(sqlite3_open_v2("MyDatabase.myExt", &m_database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr));
checkErrorCode(sqlite3_prepare_v2(m_database, CreateDatabase_sql, -1, &statement, nullptr));
checkErrorCode(sqlite3_step(statement));
checkErrorCode(sqlite3_finalize(statement));

CreateDatabase_sql[] is created by a script for embedding the .sql file.

If I run this code, sqlite3_prepare_v2 returns error 1 - SQL Logic error. If I run the query manually (for example with the DB Browser gui) it works and the table is created.

What I'm doing wrong?

Upvotes: 0

Views: 489

Answers (1)

Shawn
Shawn

Reputation: 52654

Here's a cleaned up, compilable freestanding version of your code (With better error messages, bug fixes (look up what sqlite3_step() returns), and removing a bunch of weird stuff from your sql statement (single quotes are for strings, double quotes are for identifiers but usually not needed, and why have a literal \0 char at the end?)):

#include <cstdlib>
#include <iostream>
#include <sqlite3.h>

void checkErrorCode(sqlite3 *db, int code) {
  if (code != SQLITE_OK && code != SQLITE_DONE) {
    const char *err;
    if (db) {
      err = sqlite3_errmsg(db);
    } else {
      err = sqlite3_errstr(code);
    }
    std::cerr << "Error " << code << ": " << err << '\n';
    std::exit(EXIT_FAILURE);
  }
}

int main() {
  sqlite3 *db;
  sqlite3_stmt *statement;
  const char CreateDatabase_sql[] = R"(
CREATE TABLE session_properties(user_name TEXT NOT NULL
                              , user_surname TEXT NOT NULL
                              , description TEXT NOT NULL
                              , CONSTRAINT PK_user
                                PRIMARY KEY(user_name, user_surname))
)";

  checkErrorCode(nullptr,
                 sqlite3_open_v2("test.db", &db,
                                 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
                                 nullptr));
  checkErrorCode(
      db, sqlite3_prepare_v2(db, CreateDatabase_sql, -1, &statement, nullptr));
  checkErrorCode(db, sqlite3_step(statement));
  checkErrorCode(db, sqlite3_finalize(statement));
  std::cout << "It seems to have worked.\n";
  sqlite3_close(db);
  return 0;
}

Now, running it...

$ ./a.out
It seems to have worked.

But running it again...

$ ./a.out
Error 1: table session_properties already exists

(Same results with your funky version of the statement)

So I suspect your problem comes from trying to create a table that already exists in your database. Switch to CREATE TABLE IF NOT EXISTS ... and it shouldn't give errors if that is indeed the issue.

Upvotes: 1

Related Questions