在 C++ 中创建一个 sqlite3 table
Creating an sqlite3 table in c++
我正在试验 C++,最近从 python 转过来;目前正在编写一个在 sqlite3 数据库中构建 table 的函数。
我似乎遇到了一些新手错误:
int db_build()
{
sqlite3 *db;
int rc; // This line
int sql; // This line
rc = sqlite3_open("test.db", &db);
/* Create SQL statement */
sql = "CREATE TABLE WORDS(" \
"ID INT PRIMARY KEY NOT NULL," \
"CURRENT_WORD TEXT NOT NULL," \
"BEFORE_WORD TEXT NOT NULL," \
"AFTER_WORD TEXT NOT NULL," \
"OCCURANCES INT NOT NULL);";
/* Execute SQL statement */
rc = sqlite3_exec(db, sql);
sqlite3_close(db);
return 0;
}
我的终端returns如下:
akf@akf-v5 ~/c/HelloWorld $ g++ main.cpp -l sqlite3
main.cpp: In function ‘int db_build()’:
main.cpp:30:8: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
sql = "CREATE TABLE WORDS(" \
^
main.cpp:38:29: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
rc = sqlite3_exec(db, sql);
^
main.cpp:38:29: error: too few arguments to function ‘int sqlite3_exec(sqlite3*, const char*, int (*)(void*, int, char**, char**), void*, char**)’
In file included from main.cpp:4:0:
/usr/include/sqlite3.h:379:16: note: declared here
SQLITE_API int sqlite3_exec(
^
如果我将 'int sql' 更改为 'char sql',我会遇到更多错误。知道如何让这件事继续下去吗?
您有一处语法错误。摆脱尾随 \
/* Create SQL statement */
sql = "CREATE TABLE WORDS("
"ID INT PRIMARY KEY NOT NULL,"
"CURRENT_WORD TEXT NOT NULL,"
"BEFORE_WORD TEXT NOT NULL,"
"AFTER_WORD TEXT NOT NULL,"
"OCCURANCES INT NOT NULL);";
还有一个 python-y 错误。将 int sql;
更改为:
const char sql[];
类型 const char sql[]
适用于常量字符串文字。
编辑:
为了完整起见,Hot Licks 还提示您对 sqlite3_exec
的调用必须是:
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
如果你打算用 C++ 编写代码,你最好很快了解什么是指针,什么是 "C string"。
sqlite3_exec
需要一个 C 字符串作为第二个参数,它是一个以零字节结尾的 8 位 char
值序列。它通过指向字符的指针传递——char *
.
因此您的声明需要 char * sql;
。
此外,如果您查看 the documentation,您会看到 sqlite3_exec
还有几个参数——它们不能被省略(尽管它们可以作为 NULL
传递)。
我正在试验 C++,最近从 python 转过来;目前正在编写一个在 sqlite3 数据库中构建 table 的函数。
我似乎遇到了一些新手错误:
int db_build()
{
sqlite3 *db;
int rc; // This line
int sql; // This line
rc = sqlite3_open("test.db", &db);
/* Create SQL statement */
sql = "CREATE TABLE WORDS(" \
"ID INT PRIMARY KEY NOT NULL," \
"CURRENT_WORD TEXT NOT NULL," \
"BEFORE_WORD TEXT NOT NULL," \
"AFTER_WORD TEXT NOT NULL," \
"OCCURANCES INT NOT NULL);";
/* Execute SQL statement */
rc = sqlite3_exec(db, sql);
sqlite3_close(db);
return 0;
}
我的终端returns如下:
akf@akf-v5 ~/c/HelloWorld $ g++ main.cpp -l sqlite3
main.cpp: In function ‘int db_build()’:
main.cpp:30:8: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
sql = "CREATE TABLE WORDS(" \
^
main.cpp:38:29: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
rc = sqlite3_exec(db, sql);
^
main.cpp:38:29: error: too few arguments to function ‘int sqlite3_exec(sqlite3*, const char*, int (*)(void*, int, char**, char**), void*, char**)’
In file included from main.cpp:4:0:
/usr/include/sqlite3.h:379:16: note: declared here
SQLITE_API int sqlite3_exec(
^
如果我将 'int sql' 更改为 'char sql',我会遇到更多错误。知道如何让这件事继续下去吗?
您有一处语法错误。摆脱尾随 \
/* Create SQL statement */
sql = "CREATE TABLE WORDS("
"ID INT PRIMARY KEY NOT NULL,"
"CURRENT_WORD TEXT NOT NULL,"
"BEFORE_WORD TEXT NOT NULL,"
"AFTER_WORD TEXT NOT NULL,"
"OCCURANCES INT NOT NULL);";
还有一个 python-y 错误。将 int sql;
更改为:
const char sql[];
类型 const char sql[]
适用于常量字符串文字。
编辑:
为了完整起见,Hot Licks 还提示您对 sqlite3_exec
的调用必须是:
rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
如果你打算用 C++ 编写代码,你最好很快了解什么是指针,什么是 "C string"。
sqlite3_exec
需要一个 C 字符串作为第二个参数,它是一个以零字节结尾的 8 位 char
值序列。它通过指向字符的指针传递——char *
.
因此您的声明需要 char * sql;
。
此外,如果您查看 the documentation,您会看到 sqlite3_exec
还有几个参数——它们不能被省略(尽管它们可以作为 NULL
传递)。