SQLite FMDB创建表 - 初学者iOS

ped*_*ros 7 sqlite fmdb ios

我认为这是一个简单的问题,但我没有在FMDB git页面找到答案.使用命令时:

[database executeUpdate:@"create table T_table(name text primary key, age int)"];
Run Code Online (Sandbox Code Playgroud)

FMDB或SQLite是否进行某种验证以查看该表是否已存在?

我可以在类初始化程序中调用此方法而不创建多个表吗?

对不起愚蠢的问题.

小智 15

另一种解决方案是将您的查询更改为:

create table if not exists test_table (test_no NUMBER, test_name TEXT);
Run Code Online (Sandbox Code Playgroud)

或者,您可以检查是否存在:

select sql from SQLITE_MASTER where name = 'test_table'
Run Code Online (Sandbox Code Playgroud)

看看你是否得到任何结果.


Rav*_*man 0

每当您向 FMDB 发出CREATE TABLE命令时,它都会在内部将其转换为相应的 SQLite 查询(您不必担心)。

根据 SQLite 网站上给出的官方文档,它指出:

"It is usually an error to attempt to create a new table in a database that already contains a table, index or view of the same name."

因此,如果您尝试创建另一个同名的表,SQLite 将抛出错误:

create table test_table (test_no NUMBER, test_name TEXT); //Table created

/* Now, try creating the table again */
create table test_table (test_no NUMBER, test_name TEXT); 
Run Code Online (Sandbox Code Playgroud)

您将收到以下错误。
错误:表 test_table 已存在

因此,SQLite 检查该表是否存在,它不允许有另一个同名的表。

同样,您可以参考文档以获取更多详细信息。

来源 http://www.sqlite.org/lang_createtable.html