Search code examples
sqlitecreate-table

SQLite + ATTACH Database + CREATE TABLE


In my app, I used ATTACH DATABASE to "merge" 2 databases and work on them. It works perfectly. No problem for SELECT / UPDATE / INSERT on tables on the 2 database files.

My question: How to specify which database file to use when I want to do a CREATE TABLE ?

CREATE TABLE caps (
    id VARCHAR PRIMARY KEY NOT NULL,
    name_en VARCHAR,
    status INTEGER DEFAULT (1))

EDIT: I attached my second database like that:

self.database = [FMDatabase databaseWithPath:DATABASE_READ_SANDBOX_PATH]; 
[self.database executeUpdate:[NSString
    stringWithFormat:@"ATTACH DATABASE '%@' AS db2",
    DATABASE_USER_SANDBOX_PATH]];

So the second database has a name (db2), but the first? And I want to add the table in the first.


Solution

  • The documentation says:

    Tables in an attached database can be referred to using the syntax database-name.table-name.
    The database-names 'main' and 'temp' refer to the main database and the database used for temporary tables.

    Thus:

    CREATE TABLE main.caps(...)
    CREATE TABLE db2.caps(...)