Android SQLITE foreign key column is null

Viewed 56

The project contains 2 tables one is Duser which contains all the user information and the second one is the test table. I want to pass the User's id which is the primary key in Duser Table as a foreign key in the test table. when I'm opening the database foreign key column is always null. I have also enabled foreign-key by using pragrma, Do I need to pass foreign key manually or SQLite will do it? if manually then how? Please help.

 @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("Create Table Duser(id integer primary key autoincrement,email Text, name text,password text, confirm text, number string)");
        sqLiteDatabase.execSQL("Create table test(Iid integer primary key autoincrement, name Text, f_id integer NOT NULL , Foreign key(f_id)   REFERENCES DUSER(id) on update restrict on delete restrict)");
        onConfigure(sqLiteDatabase);
    }

    @Override
    public void onConfigure(SQLiteDatabase db) {
        super.onConfigure(db);
        db.execSQL("PRAGMA foreign_keys=ON;");
    }

  public boolean Insert(String email, String name, String password, String confirm, String number) {


        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put("email", email);
        contentValues.put("name", name);
        contentValues.put("password", password);
        contentValues.put("confirm", confirm);
        contentValues.put("number", number);

        long ins = db.insert("Duser", "null", contentValues);
        if (ins == -1)
            return false;
        else return true;
    }

  public boolean addInterest(String name) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        long ins = db.insert("test", null, contentValues);

        if (ins == -1)
            return false;
        else return true;

    }
0 Answers
Related