I am trying to make a SQLite table for group of contacts, but I do know the number of contacts that are going to be in that table, so how could I account for ALL the contacts? I could make a single TEXT column and have the String URI of each contact with a delimeter. I would like to support the wide range of users running Android 1.5 and (correct me if I’m wrong) the version of SQLite that runs on Android 1.5 doesn’t support foreign keys (only Android 2.2 and above). What should I do?
Share
You can simulate foreign key constraints on older versions of android using triggers: http://www.codeproject.com/KB/android/AndroidSQLite.aspx
From there, you can simply have another table for the groups (and whatever metadata you wanted associated to it), and then another for contacts, and have each contact have a group_id, or if you want multiple groups per contact, you could also have another table containing only contact_id and group_id.
So, let’s say you want a set of contacts, and you want a set of contact groups. And you want each group to contain a variable amount of contacts, and you want a contact to be in a variable ammoun of groups, you will want three tables, a group table, a contact table, and a groupcontact table. The group table will have all of the columns that a group needs, plus an additional primary key column (note that it has to be called _id because android assumes it will be called _id). The contact table will have all of the columns needed for a contact, plus an additional primary key. Finally, the groupcontact table will have three columns (although you only care about two of them), a primary key column, a group_id column, and a contact_id column.
Finally, if you want to associate all of the elements of a group, including all of the contacts, you could do something like this:
Where and are from the groupcontact table. (Note that android has methods demonstrated in their notepad example: http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NotePadProvider.html , also you can see an example of things similar (although a bit simpler) in a recent provider I wrote (although I haven’t implemented triggers yet, so it won’t work properly in older versions of android): https://github.com/LeifAndersen/NetCatch/blob/master/src/net/leifandersen/mobile/android/netcatch/providers/ShowsProvider.java