gpt4 book ai didi

c++ - 在 C++ 中创建一个 sqlite3 表

转载 作者:IT王子 更新时间:2023-10-29 06:31:06 28 4
gpt4 key购买 nike

我正在尝试使用 C++,最近从 Python 转向了 C++;目前正在编写一个在 sqlite3 数据库中构建表的函数。

我似乎遇到了一些新手错误:

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;
}

我的终端返回以下内容:

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++ 中创建一个 sqlite3 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28133011/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com