views:

87

answers:

2

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?

+2  A: 

Are you certain that you really need a databse? 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 db or your code, it is taking up space. The db will add some overhead to that and vious?will take some time to load, plus your code has to handle errors, etc.

Woudl you not be better off with a simple array declared in your code? Or am I misisng something obvious? (maybe users can d/l a new db? But is that so much more overhead than d/ling a new program?)

If I'm way off, please explain (rather than downvoting). I am trying to help


Edit: presumably you already have your facts soemwhere? Maybe in a text file? You could just write code to parse that and initialze and array (or populate a db). It should bascially be a short for loop.

LeonixSolutions
Hi, thanks for taking the time to ans the question. Each fact has a source attached to it. So I thought of a 2 column table (3 columns including the id). I'd like to keep a track of the id so that the next time the user accesses the app, it show the next fact, from where it left off, not the first fact again. I suppose I could do a 2-d array, and keep track of the index number where it was left off in the Preferences. Not sure what the best design solution is. Hope my answer makes sense.
OceanBlue
I concur with LeonixSolutions. You have your facts somewhere. If it is a text form already (csv, xml, whatever), you can put that file in the res/raw directory and it will be available to your app when installed for use directly or populating your database.
Jere.Jones
Great!! Thanks.
OceanBlue
A: 

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

xenonite