CompEng
CompEng

Reputation: 7416

android (data base)

I want to use database in my android project. I write codes but I am curious about :when I run the apk file in my phone the database rendered again every time or it is only first time create database ? Because I want the database built only first time ? How can I do this ?

Upvotes: 0

Views: 88

Answers (2)

Mr Surfy
Mr Surfy

Reputation: 61

Your database 'helper' should at least override the onCreate method to create tables in the database one time only (when the database is first created on the device)....

public class ExampleDatabaseHelper extends SQLiteOpenHelper {

    public static final String DB_NAME = "ExampleDb";
    public static final int DB_VERSION = 1;
    public static final String TABLE_NAME = "ExampleTable";
    private static final String TABLE_CREATE_SQL = 
            "CREATE TABLE " + TABLE_NAME +" (" + 
                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "Name TEXT NOT NULL " +
            ")";

    public ExampleDatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {           
        db.execSQL(TABLE_CREATE_SQL);
    }
}

...as you develop the app it is likely you'll want to make changes to the database structure, so you might want to drop these existing tables and run new SQL to recreate them.

You can do this by overriding the onUpdate method, but you must remember to increment the database version each time you change you schema in order for onUpdate to be triggered...

   public class ExampleDatabaseHelper extends SQLiteOpenHelper {

    public static final String DB_NAME = "ExampleDb";
    public static final int DB_VERSION = 2;
    public static final String TABLE_NAME = "ExampleTable";
    private static final String TABLE_CREATE_SQL = 
            "CREATE TABLE " + TABLE_NAME +" (" + 
                "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "Name TEXT NOT NULL, " +
                "PhoneNumber TEXT NOT NULL" +
            ")";

    public ExampleDatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {           
        db.execSQL(TABLE_CREATE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

}

In the published app you probably would want to improve onUpdate to preserve data from the previous version in the new format, but while under development the above will do. This example is simple enough as it only deals with one table in the database. I would advise creating a class for each table with static methods to deal with onCreate and onUpgrade for that table, which are called from the helper class...

public class ExampleDatabaseHelper extends SQLiteOpenHelper {

    public static final String DB_NAME = "ExampleDb";
    public static final int DB_VERSION = 1;

    public ExampleDatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {   
        ExampleTable1.onCreate(db);
        ExampleTable2.onCreate(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        ExampleTable1.onUpgrade(db, oldVersion, newVersion);
        ExampleTable2.onUpgrade(db, oldVersion, newVersion);
    }
}

Upvotes: 1

barry
barry

Reputation: 4147

The database will be created the very first time you try to access it and will persist from then on, even when installing a newer version of the app.

Hope that helps.

Upvotes: 1

Related Questions