OceanBlue
OceanBlue

Reputation: 9248

Creating initial SQLiteDatabase when app is installed

I am writing an app that displays fun-facts (and the source they are from). The user can browse through the facts one by one.

Here is the design I thought of :
Create a table in SQLiteDatabase with a text column that stores the fun-fact and a second column that stores it's source. (not sure if this is the best way to go about doing it, but I want the app available even without the network)

My question is, when database is initially created on the device, should I manually populate the database from within the code, something like this pseodo-code:-

    @Override
public void onCreate(SQLiteDatabase db) {

    //Create the table

    //Populate the table
        //Insert statement 1
        //Insert statement 2
        //Insert statement 3
                          ...
        //Insert statement 500

}

Surely there must be a better method to create the initial database when the app is installed?

Upvotes: 0

Views: 2291

Answers (2)

Mawg
Mawg

Reputation: 40150

Are you certain that you really need a database? Doesn't it just add unnecessary overhead to something so trivial?

Can't you just declare the array in your code, or am I missing something? Whether it's in the database or your code, it is taking up space. The db will add some overhead to that and vious? It will take some time to load, plus your code has to handle errors, etc.

Would you not be better off with a simple array declared in your code? Or am I missing something obvious? (Maybe users can download a new database? But is that so much more overhead than downloading a new program?)

Edit

Presumably you already have your facts somewhere? Maybe in a text file? You could just write code to parse that and initialize and array (or populate a database). It should basically be a short for loop.

Upvotes: 2

xenonite
xenonite

Reputation: 1671

use a class derived from SQLiteOpenHelper

i already wrote sth obout this on my blog www.xenonite.net

public class myDatabase extends SQLiteOpenHelper
{
    private static final String     DB_NAME     = "database.db";
    private static final int    DB_VERSION  = 1;

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

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL("CREATE TABLE tbl_test ( id INTEGER PRIMARY KEY AUTOINCREMENT, test TEXT NOT NULL )");
    }

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

you can use this like

myDatabase db = new myDatabase(getApplicationContext());

sql = "INSERT INTO tbl_test (test) VALUES ('xyz')";
db.getWritableDatabase().execSQL(sql);

String sql = "SELECT id FROM tbl_test"; 
Cursor result = db.getWritableDatabase().rawQuery(sql, null);

int value;

while(result.moveToNext())
{
        value = result.getInt(0);
}

on every call to db, myDatabase.onCreate() is called, so your database will be created on the first run. when changing the table structure, implement onUpgrade() and increment DB_VERSION

Upvotes: 1

Related Questions