Vous êtes sur la page 1sur 1

There are two options for creating and updating databases.

One is to create a database externally, then place it in the assets folder of the project and then copy the entire database from there.
This is much quicker if the database has a lot of tables and other components. Upgrades are triggered by changing the database
version number in the res/values/strings.xml file. Upgrades would then be accomplished by creating a new database externally,
replacing the old database in the assets folder with the new database, saving the old database in internal storage under another
name, copying the new database from the assets folder into internal storage, transferring all of the data from the old database (that
was renamed earlier) into the new database and finally deleting the old database. You can create a database originally by using
the SQLite Manager FireFox plugin to execute your creation sql statements.
The other option is to create a database internally from a sql file. This is not as quick but the delay would probably be unnoticeable
to the users if the database has only a few tables. Upgrades are triggered by changing the database version number in the
res/values/strings.xml file. Upgrades would then be accomplished by processing an upgrade sql file. The data in the database will
remain unchanged except when its container is removed, for example dropping a table.
The example below demonstrates how to use either method.
Here is a sample create_database.sql file. It is to be placed in the assets folder of the project for the internal method or copied into
the "Execute SQL' of SQLite Manager to create the database for the external method. (NOTE: Notice the comment about the
table required by Android.)
--Android requires a table named 'android_metadata' with a 'locale' column
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US');
INSERT INTO "android_metadata" VALUES ('en_US');
CREATE
CREATE
CREATE
CREATE
CREATE

TABLE
TABLE
TABLE
TABLE
TABLE

"kitchen_table";
"coffee_table";
"pool_table";
"dining_room_table";
"card_table";

Here is a sample update_database.sql file. It is to be placed in the assets folder of the project for the internal method or copied into
the "Execute SQL' of SQLite Manager to create the database for the external method. (NOTE: Notice that all three types of SQL
comments will be ignored by the sql parser that is included in this example.)
--CREATE TABLE "kitchen_table"; This is one type of comment in sql. It is ignored by parseSql.
/*
* CREATE TABLE "coffee_table"; This is a second type of comment in sql. It is ignored by parseSql.
*/
{
CREATE TABLE "pool_table"; This is a third type of comment in sql. It is ignored by parseSql.
}
/* CREATE TABLE "dining_room_table"; This is a second type of comment in sql. It is ignored by parseSql. */
{ CREATE TABLE "card_table"; This is a third type of comment in sql. It is ignored by parseSql. }
--DROP TABLE "picnic_table"; Uncomment this if picnic table was previously created and now is being replaced.
CREATE TABLE "picnic_table" ("plates" TEXT);
INSERT INTO "picnic_table" VALUES ('paper');

Vous aimerez peut-être aussi