SQLiteOpenHelper, when is onCreate called
I'm toying with the Android SDK, with a view to writting a simple app for friends (and maybe for sale).
This app will search a 开发者_运维问答database for keywords and dispaly the results on the screen, I've had a look at the searchabul dictonary and the notepad demo applications, but I'm still a bit unsure some things.
I know I need to write a class that extends the SQLiteOpenHelper, and use that to create the database, however how do I check if the database already exists?
Does onCreate get called on installation or every time an instance of the class is created? is the easyist way just to try and add the database each time and catch any errors (feels a little dangerous to be makeing the assumption every error will be due to the database already existing).
Thanks in Advance.
Your SQLiteOpenHelper will handle the creation of the database. Once the db has been created on the users phone SQliteHelper will be happy until you change the database version number.
As long as you put your db creation code in the onCreate method of a SQLiteOpenHelper you will be fine.
Have a look at the onUpgrade method from the Notepad demo as you need to write some code that handles what happens when your db tries to upgrade itself (it will do this when you change the db version number)
Whenever you call helper.getWritableDatabase() , onCreate method is called.
精彩评论