Android sqlite database for prefilling

I have a sqlite database in my application. My application should not send this as an empty database, but some records must be created before the user can use the application for the first time. Of course, I can have many false statements, but this seems very strange. I was thinking of something like http://vineetyadav.com/tutorials/11-adding-prefilled-sqlite-database-to-android-app.html , which basically works, but that means for my manually created DB all internal tables like android_metadata are needed. Therefore, I need to know which internal tables exist and how to populate them. Or another smart way to have a pre-populated database. Any suggestions?

Thanks A.

+5
source share
3 answers

You can read in a flat file from an SD card with 1 insert for each line and scroll through this file.

Here is an example in which I read the file and add 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 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;
}
+3
source

Personally, I just did the inserts, but I had only 2, so this did not seem to me.

If you need to know the internal tables, why don't you just extract the database from your application and stop it from looking at any SQLite database browser (I use SQLite Database Browser )?

android_metadata is displayed in the SQLite Database Browser as created, for example CREATE TABLE android_metadata(locale TEXT).

Perhaps insert instructions to make sure they look.

Then you can easily create it and add it?

+1

I saved the data in the res / raw directory as JSON. When the application was opened for the first time, I used AsyncTask, read the JSON file (used the GSON library) and populated my database with it.

I tried this for about 1000 entries and it worked for me. This went on for some time - about 15-30 seconds on the emulator.

Not sure if this is the best way.

+1
source

All Articles