• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps SQLite records pre defined

creatiive

Lurker
hi,

I've been reading a bunch of tutorials explaining how to create your SQLite database if it isn't already there, and adding records to it etc. What im looking for is how to have records already in the database when it is installed?

if anyone could point me in the direction of a tutorial or some resource that explains how to achieve this , it would be much appriciated!!

thanks
 
The easiest way to do this is when you're creating your database.

If the database isn't there you need to run your script to create the database and then run a script to do your INSERT statements on the database that you just created adding the records.
 
but i only ever want the INSERTs to run once - i dont want to have to have that re-execute every time the app is opened, even if they dont actually do any inserting because they keys are already in there, it would still take processing (and i have a lot of records).#


is it possible to only run the INSERTs if the database gets created, and not if its already there?
 
You don't want to create the database every time the app is opened. The first time you open the app . . it will create the database . . and add the records.

The next time the app is opened . . the database will be there so it won't have to go through that step again.
 
but i only ever want the INSERTs to run once - i dont want to have to have that re-execute every time the app is opened, even if they dont actually do any inserting because they keys are already in there, it would still take processing (and i have a lot of records).#


is it possible to only run the INSERTs if the database gets created, and not if its already there?

in your same logic where you determine if you need create the database because it doesn't exist, that is when you would do the insert. therefore, you would only be inserting the records when you have to create the database.
 
Are you using a SQLiteOpenHelper derived class to open/create your database?

The SQLiteOpenHelper has an abstract method, onCreate(), which is called if the database needs to be created. You can execute your INSERT statements in this method after you create the database. This method is only called the first time an application needs to create a database. Next time the application runs, the database file already exists and SQLiteOpenHelper skips the call to onCreate().
 
Back
Top Bottom