[英]SQLite3: the database was created empty?
I'm trying to create a database with a table using sqlite3 on my C program, however the database is always created as empty, though it was created non-empty using the sqlite shell, here is my code below: 我试图在我的C程序上使用sqlite3创建一个带有表的数据库,但是该数据库始终创建为空,尽管它是使用sqlite shell创建的非空数据库,这是我的以下代码:
int main(void)
{
printf("hello\n");
sqlite3 *sqdb;
sqlite3_initialize();
const char* db = "test";
sqlite3_open(db, &sqdb);
const char* stmt = "CREATE TABLE IF NOT EXISTS testtable(creationdate DATE, data VARCHAR);";
sqlite3_stmt *ppstmt;
if (sqlite3_prepare_v2(sqdb, stmt, -1, &ppstmt, 0)!=SQLITE_OK)printf("error!\n");
sqlite3_finalize(ppstmt);
getch();
return 0;
}
please help me to solve the problem. 请帮助我解决问题。
sqlite3_prepare_v2()
alone just compiles the SQL but does not run it. sqlite3_prepare_v2()
仅会编译SQL,但不会运行它。 Call sqlite3_step()
on the compiled statement to run it, or use sqlite3_exec()
that combines prepare+step+finalize into one function call. 在已编译的语句上调用
sqlite3_step()
来运行它,或使用将prepare + step + finalize组合为一个函数调用的sqlite3_exec()
。
Try this: 尝试这个:
int main(void)
{
printf("hello\n");
sqlite3 *sqdb;
int ret;
sqlite3_initialize();
const char* db = "test.sqlite3";
sqlite3_open(db, &sqdb);
const char* stmt = "CREATE TABLE IF NOT EXISTS testtable(creationdate DATE, data VARCHAR);";
sqlite3_stmt *ppstmt=NULL;
ret=sqlite3_exec(sqdb,stmt,0,0,0);
if(ret!=SQLITE_OK)printf("error!\n");
else printf("Table added\n");
sqlite3_finalize(ppstmt);
sqlite3_close(sqdb);
return 0;
}
Please do remember to close the DB after operation. 请记住操作后要关闭数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.