Kula Sekhar
Kula Sekhar

Reputation: 1

Android: What is the best place to create SQLite database and tables

I have to create nearly 5-6 tables in my android application. What is the best place to create SQLite database and 5-6 tables? I can write all these logic(Using SQLite open helper methods) when initial activity launch, but if i do that it will be called every time initial activity launches. Which means these creation logic of database and tables will be executes every time application launches.Can any body tell the best practice to do this.

Upvotes: 0

Views: 1074

Answers (4)

F. Geraerts
F. Geraerts

Reputation: 1190

You can use an inner class extendings the SQLiteOpenHelper and override the methods public void onCreate(SqLiteDatabase sqlLiteDatabase) and public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion)

The onCreate is called when no database exists in disk and the helper class needs to create a new one

The onUpgrade is called when there is a db version mismatch. The version of db on disks needs to be upgraded to the newVersion

private static final class PatientDatabaseHelper extends SQLiteOpenHelper {

    private static final String TAG = "PatientProvider";

    private static final String DATABASE_NAME = "myPatient.db";
    private static final int DATABASE_VERSION = 1;
    private static final String PATIENT_TABLE = "PATIENT";

    private static final String CREATE_TABLE_PATIENT =
            "create table " + PATIENT_TABLE + " ("
                    + PATIENT_KEY_ID + " integer primary key autoincrement, "
                    + PATIENT_NAME + " TEXT NOT NULL, "
                    + PATIENT_FIRST_NAME + " TEXT NOT NULL, "
                    + PATIENT_TEL + " TEXT, "
                    + PATIENT_GSM1 + " TEXT, "
                    + PATIENT_GSM2 + " TEXT, "
                    + PATIENT_BIRTHDATE + " DATE); ";



    public PatientDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE_PATIENT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
       //simple case sample - droppin the table
       sqLiteDatabase.execSQL("DROP TABLE IF IT EXISTS " + PATIENT_TABLE);


      //Create a new One
      onCreate(sqLiteDatabase);

    }
}

Depends of the target version of android but i think a good approach is to use with that a ContentProvider http://developer.android.com/guide/topics/providers/content-provider-creating.html

Upvotes: 0

Joey Roosing
Joey Roosing

Reputation: 2155

If you write a class to extend SQLiteOpenHelper and create your tables in the onCreate method it will only be executed once. The helper will only recreate the data if you completely remove the data/application from the device.

From developer.android.com: Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.

To give you some more info. In the constructor of the class that you use to extend SQLiteOpenHelper, call the super constructor, which accepts a few parameters. One of them being the database version.

If this db_version passed to the super constructor is different then the last passed one, the onUpgrade method is called, in the onUpgrade() method you can call onCreate()

so:

  1. delete data/app
  2. cause onUpgrade to be called which calls onCreate()

are the ways to create/renew/delete your database

Upvotes: 1

Skies
Skies

Reputation: 415

Personnaly, I use the option : CREATE TABLE IF NOT EXISTS.

So th open helper have this param :

 private static final int VERSION_BDD = 1;

I think the openHelper don't recreate each table at each launch.

Upvotes: 0

MAC
MAC

Reputation: 15847

You can create database and tables in main activity

But before creating database you can check

if(exits())
{
   // do not create
}
else
{
   // create here
}

Upvotes: 0

Related Questions