Search code examples
iossqlitefmdb

SQLite FMDB creating table - beginner iOS


I think this is a simple question but I didnt find the answer in 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 make some kind of verification to see if the table already exists?

Can I call this method in my class initializer without creating more than one table?

Sorry if stupid question.


Solution

  • Whenever you give the CREATE TABLE command to FMDB, it internally converts it into corresponding SQLite query (for which you don't have to worry).

    As per the official documentation given on SQLite's 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 will throw an error saying:

    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: table test_table already exists

    So, SQLite checks for the existence of the table, it won't allow another table with the same name.

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

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