There is a class which contains two fields of type String , and lists of objects.

I can add the contact myself in this way:

 public boolean insertContact(String name) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(CONTACTS_COLUMN_NAME, name); db.insert("contacts_caling", null, contentValues); return true; } 

And how can you add a contact with an attached object or list if they are linked by COLUMN_CONTACT_ID . Those. in order to add a contact with all the content, you must first add it yourself, then re-make the request and find out what _id it _id , then add the nested objects by setting the already known field COLUMN_CONTACT_ID , or can this be done as a single request?

Here is an example of a Contact table and for example an Email table

Contact.java

 public class Contact { private String id; private String displayName; private ArrayList<Phone> phone; private ArrayList<Email> email; private ArrayList<String> notes; private ArrayList<Address> addresses = new ArrayList<Address>(); private ArrayList<IM> imAddresses; private Organization organization; } 

TableContact.java

 public class ContactTable { // Database table public static final String TABLE_CONTACT = "contacts"; public static final String COLUMN_ID = "id"; public static final String COLUMN_NAME = "name"; // Database creation SQL statement private static final String DATABASE_CREATE = "create table " + TABLE_CONTACT + "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_NAME + " text" + ");"; public static void onCreate(SQLiteDatabase database) { database.execSQL(DATABASE_CREATE); } public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { Log.w(ContactCalingTable.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); database.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT); onCreate(database); } } 

TableContactEmail.java

 public class EmailTable { // Database table public static final String TABLE_CONTACT_CALING_EMAIL = "contact_email"; public static final String COLUMN_ID = "id"; public static final String COLUMN_CONTACT_ID = "contact_id"; public static final String COLUMN_ADDRESS = "address"; public static final String COLUMN_TYPE = "type"; // Database creation SQL statement private static final String DATABASE_CREATE = "create table " + TABLE_CONTACT_EMAIL + "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_CONTACT_ID + " integer, " + COLUMN_ADDRESS + " text, " + COLUMN_TYPE + " text" + ");"; public static void onCreate(SQLiteDatabase database) { database.execSQL(DATABASE_CREATE); } public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { Log.w(ContactCalingTable.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); database.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT_EMAIL); onCreate(database); } } 

Add. question: is it necessary in this case to do COLUMN_CONTACT_ID as a foreign key , or not?

    1 answer 1

    foreign key appeared only with Android 2.2, and this feature should be specifically included, as I recall. Therefore, I would say that you do not need this.

    The answer to the main question: yes, you need to do extra request. db.insert(...) returns the ROWID on which your primary key is received and use it to insert it into the email table.