Search code examples
androidcontent-values

How can I insert data into the SQLITE database with a foreign key using a content values?


I have a table below:

+=================+
+ CATEGORY_TABLE  +
+=================+
+ PK | owner_id   +
+ FK | task_id    +
+    | lastname   +
+    | firstname  +
+    | etc...     +
+=================+

+=================+
+   TASK_TABLE    +
+=================+
+ PK | task_id    +
+ FK | task_title +
+=================+

What I want to know is how can I insert data into my Owner table and put as well the task_id FK from my TASK_TABLE? This is using contentvalues.

Please check my snippet code below:

    //insert data to account table
public  boolean insert_account(String acc_uname, String acc_email, String acc_passw, String acc_type, String acc_status){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("acc_uname", acc_uname);
    contentValues.put("acc_email", acc_email);
    contentValues.put("acc_passw", acc_passw);
    contentValues.put("acc_type", acc_type);
    contentValues.put("acc_status", acc_status);

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

Solution

  • Try to use triggers in sqlite

    An SQLite trigger is a named database object that is executed automatically when an INSERT, UPDATE, or DELETE statement is issued against the associated table.

    check out this link to learn more about triggers