0

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kumisku
  • 103
  • 4
  • 17
  • You should see a log in logcat if table creation fails. Which Android Version do you use? As described in a comment in this [stack overflow question](http://stackoverflow.com/questions/2545558/foreign-key-constraints-in-android-using-sqlite-on-delete-cascade) foreign keys are not supported below Android 2.2 – thaussma Oct 28 '11 at 12:44
  • i use 2.2 . is there another way to create foreign key in 2.2? – kumisku Oct 28 '11 at 12:51
  • If you're not targeting older versions: http://stackoverflow.com/questions/2545558/foreign-key-constraints-in-android-using-sqlite-on-delete-cascade otherwise you'll have to write TRIGGERs that simulate a foreign key constraint - Google will help you readily with that. – Jens Oct 28 '11 at 14:11
  • thanks Jens, i solved it already :) – kumisku Oct 30 '11 at 14:00

3 Answers3

3

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

kumisku
  • 103
  • 4
  • 17
0

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

bapi
  • 98
  • 1
  • 1
  • 6
0

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.

Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149