How to insert data into two tables at once using SQLiteDatabase in Android?
From the documentation for INSERT in SQLite:
Note that the flowchart doesn't give you an opportunity to post into more than one table at once. SQLiteDatabase.insert()
is a convenience method for using the INSERT statement.
You can simulate this using database transaction:
SQLiteDatabase db = getWritableDatabase(); db.beginTransaction(); try { db.execSQL("insert into table1..."); db.execSQL("insert into table2..."); db.setTransactionSuccessful(); } finally { db.endTransaction(); }
You may need to choose the order of the inserts if there are dependencies between the values inserted, for instance if there's a foreign key relationship: ( insert to table1 and then table2, or first insert to table2...)