Before a database can be used you must, of course, create a schema for it. You can either use standard SQL commands to create the tables in the database or you can use one of the many SQLite graphical tools for this. I like to use the SQLite Manager add-on for Firefox. Another option is the SQLite Administrator tool.
After creating your database file with one of these tools, you then create the schema.
*** Describe foreign key work-around. ***
Once the schema is created then you can use the SQLite tool to generate the necessary SQL commands to create your database schema. This SQL can then be copied into your Android app so you can create the SQLite database on the end-user’s device at run-time. The SQLite Manager tool has the following commands (on the File menu) which are useful for this:
Export Database Structure—Generates SQL for the tables and triggers.
Export All Tables—Generates SQL for the tables (including a drop statements for each) and the table data.
Export Database—Generates SQL for the tables (including a drop statements for each), the table data, and any triggers.
Android reserves the table named android_metadata. This table can be created with SQL commands, as follows:
or the setLocale method can be called after the database has been created.
Once I have the database schema built I like to put it into a raw text file resource for my project. In Eclipse, right-click the project’s res/raw directory and select New → File. Enter a name for the file, like my_schema_v1.sql. This will create a resource for you named R.raw.my_schema_v1.sql. You might use LineNumberReader to load the schema in the onCreate method of your class that extends the SQLiteOpenHandler. Using a LineNumberReader allows you to provide very precise error messages if there are any problems processing the schema on the device. For example:
Now whenever you need to update this schema just change the contents of the raw file and ADT takes care of the rest.
Why aren’t UTF-8 characters (like ’) being preserved in the SQL text stirngs?
After creating a SQLite database on an Android device, a special table named android_metadata will exist. This table contains one column named locale. The table also contains a single row with the locale field set to the locale that the device has defined. In the U.S. the locale field will contain the value en_US.
Controls whether or not a database is thread-safe or not. By default, locking is enabled and the database is thread-safe. If you know that the database will be accessed by only one thread for a given operation then you can disable locking.