I have a sqlite database in my application. My application shouldn't ship this as an empty database but certain records need to be created before the user can use the application the first time. Of course I can have many insert statements, but that seems to be very strange. I thought of something like http://vineetyadav.com/tutorials/11-adding-prefilled-sqlite-database-to-android-app.html which basically works, but that means that my manually created DB needs all internal tables like android_metadata as well. So I either need to know what internal tables exist and how to fill them. Or another smart way of having a pre-filled database. Any suggestions?
Thanks, A.
You could read in a flat file from the SD card with 1 insert statement per line and iterate through that file.
Here's a similar example in which I read a file and add a ContactItem to my contact_list:
public ArrayList<ContactItem> readInputFile(Context context, String filename) {
String text = null;
BufferedReader reader = null;
ArrayList<ContactItem> contact_list = new ArrayList<ContactItem>();
contact_list.clear(); // Clear any existing contacts when a new file is read
try {
reader = new BufferedReader(new FileReader(filename));
// Repeat until EOF
while (((text = reader.readLine()) != null)) {
if (!(text.length() > 1024)) { // If we read more than 1k per line there's a problem with the input file format
ContactItem c = new ContactItem(text);
if (c.getIsValid()) {
// We were able to parse a well formed phone number from the last line read
contact_list.add(c);
}
}
}
} catch (FileNotFoundException e) {
Toast.makeText(context, R.string.error_fileNotFound, 1).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(context, R.string.error_ioException, 1).show();
e.printStackTrace();
} finally { // EOFException (or other, but EOF is handled and most likely)
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
Toast.makeText(context, R.string.error_generalFailure, 1).show();
e.printStackTrace();
}
}
return contact_list;
}
I stored the data in the res/raw directory as JSON. When the application was opened for the first time, I used an AsyncTask, read the JSON file (used the GSON library) and filled my database with it.
I have tried this with around 1000 records and it worked for me. It still took some time - around 15-30 seconds on an emulator.
Not sure if this is the best way.
Personally I just did the insert statements, but I only had 2 so it didn't seem over the top for me.
If you need to know the internal tables why don't you just extract the database from your application and give it a look with any SQLite database explorer (I use SQLite Database Browser)?
android_metadata is shown in the SQLite Database Browser as being created by CREATE TABLE android_metadata(locale TEXT)
for example.
Possibly insert a statement to make sure that's how it would look.
Then you can easily recreate it and add it?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With