0

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;
}
no_profile
  • 387
  • 5
  • 15
  • 1
    Typically, you would insert into your TASK_TABLE first. Your task_id is probably an autoincrementing value assigned by the database. Then, you query SQLite again to get the last_insert_id by running the SQL Query select last_insert_rowid() against the same SQLite database. Then, you would run your insert into the CATEGORY_TABLE using the value you received from the last_insert_rowid() call. – Michael Dougan Jul 16 '19 at 16:02

1 Answers1

1

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

Hossam Hassan
  • 665
  • 1
  • 8
  • 22