kumisku
kumisku

Reputation: 103

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

Upvotes: 0

Views: 4939

Answers (3)

kumisku
kumisku

Reputation: 103

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

Upvotes: 3

Yaqub Ahmad
Yaqub Ahmad

Reputation: 27659

The db.execSQL(Table_CREATE_SQL) should work fine in this case but it has no means to return any data, however it throws SQLException if the SQL string is invalid.

You can pull out the DB & use SQLite browser to check the tables.

Upvotes: 0

bapi
bapi

Reputation: 98

To see the data manually (not programmatic) from database you can use SQLiteManager plugins of Firefox

Upvotes: 0

Related Questions