Search code examples
androiddatabasesqlitecreate-table

Android sqlite multiple tables


I want to create 2 tables in a sqlite db. I pulled the db from emulator to pc but the second table can't be created just first table was created successfully. Can I see the table successes to be created or not even the data has not been entered? Or I've to enter the data first then I can see it manually by pulling the db from emulator first?

Here is my code to create multiple table

db.execSQL("CREATE TABLE almag (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, jekel TEXT);");
db.execSQL("CREATE TABLE score (_id INTEGER PRIMARY KEY AUTOINCREMENT, score INTEGER, userId INTEGER FOREIGN KEY REFERENCES almag (_id));"); //create table score

Solution

  • I already solved it, to add foreign key in android 2.2 just need to add these code

    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE almag (_id INTEGER PRIMARY KEY AUTOINCREMENT, nama TEXT, jekel TEXT);");
        db.execSQL("CREATE TABLE score (_id INTEGER PRIMARY KEY AUTOINCREMENT, score INTEGER, userId INTEGER NOT NULL, FOREIGN KEY (userId) REFERENCES almag(_id) ON DELETE CASCADE);"); //create table score
        db.execSQL("PRAGMA foreign_keys = ON;");
    }
    

    reference http://panierter-pinguin.de/blog/?p=138