I created two sqlite tables in android
phone table
with primary key "id"
CREATE TABLE BLOCKED_PHONES_TABLE ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 1, KEY_PHONE TEXT UNIQUE,KEY_IS_BLOCKED BIT )
comment table
with foreign key "id"
CREATE TABLE COMMENTS_TABLE ( id INTEGER, KEY_COMMENT_TEXT TEXT, FOREIGN KEY(id) REFERENCES BLOCKED_PHONES_TABLE(id))
code:
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_BLOCKED_PHONES_TABLE =
"CREATE TABLE "+ BLOCKED_PHONES_TABLE +
" ( "+ KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 1, "
+ KEY_PHONE + " TEXT UNIQUE,"
+ KEY_IS_BLOCKED+" BIT )";
db.execSQL(CREATE_BLOCKED_PHONES_TABLE);
}
and
@Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create book table
String CREATE_COMMENTS_TABLE =
"CREATE TABLE " + COMMENTS_TABLE +
" ( "+ KEY_ID+" INTEGER, "
+ KEY_COMMENT_TEXT+" TEXT, "
+ "FOREIGN KEY("+KEY_ID+") REFERENCES "+PhoneDal.BLOCKED_PHONES_TABLE+"("+KEY_ID+"))";
db.execSQL(CREATE_COMMENTS_TABLE);
}
I added some data.
including 2-3 comments to the same phone.
why does the comment table
don't refer id
as a foreign key?
otherwise it won't have ids that are missing in the phone
table.
how can I know my sqlite version?
Edit I added:
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("PRAGMA foreign_keys=ON;");
phoneDal.onCreate(sqLiteDatabase);
commentDal.onCreate(sqLiteDatabase);
}