6

I've checked the NotesDbAdapter example, and I noticed that is for a single table only. (notes)

1) I am wondering how would this be done for multiple tables? For example I have 3 tables for now. How would I handle all the upgrade, fetch, delete from these three tables?

2) I see defined properties for the table columns

public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_ROWID = "_id";

How would you define the columns for a multiple table situation?

Pentium10
  • 204,586
  • 122
  • 423
  • 502

1 Answers1

6

For my application I created a class to define the database :

public final class MyApplicationDb
{
    public static String DATABASE_NAME = "my_application_db";
    public static int DATABASE_VERSION = 1;

    public static final class Table1
    {
        public static String TABLE_NAME = "table1";
        public static String ID = "_id";
        public static String DAY = "day";
        public static String NAME = "name";
        public static String SOURCE = "source";
        public static String[] COLUMNS = { ID, DAY, NAME, SOURCE };
    }
    public static final class Table2
    {
        public static String TABLE_NAME = "table2";
        public static String ID = "_id";
        public static String CONTACT_ID = "contactId";
        public static String CONTACT_NAME = "contactName";
        public static String LAST_WISH_DATE = "lastWishDate";
        public static String[] COLUMNS = { ID, CONTACT_ID, CONTACT_NAME, LAST_WISH_DATE };
    }
}

And 2 sql scripts for creating and updating the db res/raw/db_create.sql and res/raw/db_update.sql

/* db_create.sql */
create table table1(
  _id integer primary key autoincrement,
  day char(5) not null,
  name varchar(64) not null,
  source varchar(64) not null);
create table table2(
  _id integer primary key autoincrement,
  contactId integer not null,
  contactName text not null,
  lastWishDate char(10) null);


/* db_update.sql */
DROP TABLE IF EXISTS table1;
DROP TABLE IF EXISTS table2

And Another class for creating/updating/accessing the database which is similar to NotesDbAdapter except it uses the sql script to create/update the database instead of constants. (look here to see how to do that)

Community
  • 1
  • 1
tbruyelle
  • 12,895
  • 9
  • 60
  • 74
  • The onUpgrade event fires once for all database or fires separately for each table class? – Pentium10 Feb 26 '10 at 09:23
  • `onUpgrade()` is triggered when Android detects that `DATABASE_VERSION` has been incremented. You can do what you want inside. Mostly you delete all the tables (see db_update.sql script) and call `onCreate()` to recreate the database. – tbruyelle Feb 26 '10 at 09:49
  • Is not advised to create 1 MyApplicationDb and several new Table1 classes for each of the table? In c# I created a class for each of the table I had, and I haven't wrapped into a adapter. – Pentium10 Feb 26 '10 at 11:06
  • that's exactly what I do, the tables are class inside the MyApplicationDb class. They are just used as references when performing a query. – tbruyelle Feb 26 '10 at 11:25