SQLite FMDB creates table - iOS beginner

I think this is a simple question, but I did not find the answer on the FMDB git page. When you use the command:

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

Does FMDB or SQLite do some kind of check to see if the table exists?

Is it possible to call this method in my class initializer without creating more than one table?

Sorry if stupid question.

+6
source share
2 answers

Whenever you pass the CREATE TABLE command to FMDB, it internally converts it to the corresponding SQLite query (for which you need not worry).

According to the official documentation provided on the SQLite website, it states:

"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."

So, if you try to create another table with the same name, SQLite throws an error:

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

You will get the following error.
Error: test_table already exists

So, SQLite checks for a table, it will not allow another table with the same name.

Again, you can refer to the documentation for more details.

Source http://www.sqlite.org/lang_createtable.html

0
source

Another solution is to modify your request:

 create table if not exists test_table (test_no NUMBER, test_name TEXT); 

or, you can check availability with:

 select sql from SQLITE_MASTER where name = 'test_table' 

And see if you get the results back.

+14
source

All Articles