如何检查sqlite3 c ++ API中是否存在表? [英] How do I check if a table exists in sqlite3 c++ API?
本文介绍了如何检查sqlite3 c ++ API中是否存在表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在打开一个数据库文件,并可能在不存在的情况下创建它。
I'm opening a database file and potentially creating it if it doesn't exist.
但是由于某种原因,这并没有创建该表。有想法吗?
But for some reason, this doesn't create the table. Any ideas?
const char* sql = "CREATE TABLE IF NOT EXISTS blocks(id text primary_key,length numeric)";
sqlite3_stmt *stmt;
rc = sqlite3_prepare_v2(db_, create_table_sql, -1, &stmt, NULL);
rc = sqlite3_step(stmt);
我还没来这里,是的,我正在检查每个点的返回码。
没有错误。
I haven't got it in here by yes I'm checking the return code at each point. There are no errors.
也许有更好的方法可以做到这一点?
Perhaps there is a better way to accomplish this?
推荐答案
另一个给定答案的变化:
Variation on another given answer:
select count(type) from sqlite_master where type='table' and name='TABLE_NAME_TO_CHECK';
如果表不存在,则返回0,如果不存在,则返回1。
Will return 0 if table does not exist, 1 if it does.
这篇关于如何检查sqlite3 c ++ API中是否存在表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文