我正在尝试创建一个SQLite数据库。创建一个新文件,然后创建该表:
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')
)
;
这是我的代码:
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[]
由用于嵌入.sql文件的脚本创建。
如果我运行此代码,则sqlite3_prepare_v2
返回错误1 - SQL Logic error
。如果我手动运行查询(例如使用DB Browser gui),查询将起作用并创建表。
我做错了什么?
答案 0 :(得分:1)
这是一个经过清理,可编译的独立代码版本(具有更好的错误消息,错误修复(查找sqlite3_step()
返回的内容),并从您的sql语句中删除了一堆奇怪的东西(单引号用于字符串,双引号用于标识符,但通常不需要,为什么在末尾加上文字\0
字符?)):
#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;
}
现在,运行它...
$ ./a.out
It seems to have worked.
但是要再次运行...
$ ./a.out
Error 1: table session_properties already exists
(与语句的时髦版本相同的结果)
因此,我怀疑,您的问题来自尝试创建数据库中已存在的表。切换到CREATE TABLE IF NOT EXISTS ...
,如果确实存在问题,它应该不会给出错误。