gpt4 book ai didi

c++ - "delete from table"不删除表?

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

我创建了“database.db”,一切正常,但为什么它最后不删除表呢?每次运行它时,我都会在创建表时收到“表已存在”错误消息。

int main()
{
sqlite3 *db; //Database Handle
char *zErr;
int rc;
char *sql;

rc = sqlite3_open("database.db", &db);

if(rc)
{
cout << "Can't open database: " << sqlite3_errmsg(db) << endl;;
sqlite3_close(db);
exit(1);
}

sql = "create table test(PID int primary key, Name text)"; //sql query

rc = sqlite3_exec(db, sql, NULL, NULL, &zErr); //execute sql statement

if(rc != SQLITE_OK)
{
if (zErr != NULL)
{
cout << "SQL error: " << zErr << endl;
sqlite3_free(zErr);
}
}
else
{
sql = "insert into test values (1,'John')";
rc = sqlite3_exec(db, sql, NULL, NULL, &zErr);

sql = "insert into test values (2,'Smith')";
rc = sqlite3_exec(db, sql, NULL, NULL, &zErr);
}



//delete the table on exit.

rc = sqlite3_exec(db, "delete from test", NULL, NULL, &zErr);

sqlite3_close(db);
return 0;
}

另外,主键是否可以在数据库中存在的最后一个更大的键之后自动生成?

最佳答案

您需要使用 drop table . delete删除表中的行。

rc = sqlite3_exec(db, "drop table test", NULL, NULL, &zErr);

SQLite 将自动递增声明为主键的整数字段。参见 here .

关于c++ - "delete from table"不删除表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4570135/

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