Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android pre-fill sqlite database

Tags:

android

sqlite

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.

like image 593
AndyAndroid Avatar asked Mar 10 '11 14:03

AndyAndroid


3 Answers

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;
}
like image 131
Bill Mote Avatar answered Nov 03 '22 08:11

Bill Mote


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.

like image 26
Abhinav Avatar answered Nov 03 '22 08:11

Abhinav


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?

like image 1
Codemonkey Avatar answered Nov 03 '22 07:11

Codemonkey