Reputation: 2468
Based on http://developer.android.com/guide/topics/data/data-storage.html#db it seems like I'd need another Java Class file (which I made), but now I've run into a rather sad problem: I'm not sure how to handle it.
How would I call it in my main activity and would it only create the database once, or would it try to re-create the database every time it runs?
Upvotes: 1
Views: 9223
Reputation: 1532
An SQLiteOpenHelper has 2 very useful methods: onCreate() and onUpgrade().
There is also onDowngrade() and onOpen() when the DATABASE_VERSION decreases or the database is opened, respectively.
onCreate() is where you want to do your db.execSQL("CREATE TABLE ....") to first make your tables.
You can then use your SQLiteOpenHandler's instance to do dbHandler.getWritableDatabase() and dbHandler.getReadableDatabase() which return a readable or writable SQLiteDatabase object in your code. You can then writableDb.insert() or readableDB.query() to put and get stuff from your db.
Upvotes: 0
Reputation: 4970
This would 'create' the database only for the first time. After that you can use an object of that SQLiteOpenHelper class to access that database. Note that you don't have to worry about the recreation of the database, android will take care of that if you use the SQLiteOpenHelper approach.
Taking the data-storage example forward you would create an object of that and then use the getWriteableDatabase method on that to get write access to the database
DictionaryOpenHelper myDictionary;
myDictionary = new DictionaryOpenHelper(this);
SQLiteDatabase db = myDictionary.getWritableDatabase();
//to enter values in the db
ContentValues values = new ContentValues();
values.put("name","Rhyno");
db.insert(nameOfTable, null, values);
For a more detailed example please refer to this tutorial: http://marakana.com/forums/android/examples/55.html
Upvotes: 4