Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Pre-Populated Database [duplicate]

I am working on an Android application that will need several entries (a single table, with 1000-10000 rows) populated in that app's database before the user can use that app. I've looked around some tutorials and I am unsure of the best way to do this. Should I just check if the database exists each time the app is started and, if it isn't there, create it and insert the thousands of records I need? Or is there a better way to handle this problem? Ideally, it could be included as part of the app's install process, but I'm not sure if this is possible. Any feedback would be greatly appreciated.

like image 412
Blather Avatar asked Mar 09 '10 12:03

Blather


2 Answers

Here is an example of how to create and populate a database, you can just do this on the app install, this only creates one entry though so may be inefficient for what you want to do.

private static class settingsDatabaseHelper extends SQLiteOpenHelper{      //SQL String for creating the table required     private static final String CREATE_SETTINGS_TABLE     = "CREATE TABLE tbl_settings(" +             "_ID INTEGER PRIMARY KEY AUTOINCREMENT," +             "VOIPUSERNAME TEXT," +             "VOIPAUTHID TEXT," +             "PASSWORD TEXT," +             "VOIPDISPLAYNAME TEXT," +             "SIPPROXYSERVER TEXT," +             "SIPREGISTRAR TEXT," +             "SIPREALM TEXT," +             "EXPIRESTIME INTEGER);";          //constructor     public settingsDatabaseHelper(Context context, String name,             CursorFactory factory, int version) {         super(context, name, factory, version);      }      @Override     public void onCreate(SQLiteDatabase db) {         db.execSQL(CREATE_SETTINGS_TABLE);          ContentValues initialValues = new ContentValues();             initialValues.put("VOIPUSERNAME", "xxxxx");             initialValues.put("VOIPAUTHID", "xxxxxxxxxx");             initialValues.put("PASSWORD", "xxxxxx");             initialValues.put("VOIPDISPLAYNAME", "xxxxxxxxx");             initialValues.put("SIPPROXYSERVER", "xxxxxxxxxxxxx");             initialValues.put("SIPREGISTRAR", "xxxxxxxxxxx");             initialValues.put("SIPREALM", "xxxxxxxxxx");             initialValues.put("EXPIRESTIME", xxxxxxxxxxx);             Log.d("1.6", "gets to here");             db.insert(SETTINGS_TABLE, null, initialValues);      }      @Override     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {         Log.w(TAG, "Upgrading database from version " + oldVersion + " to " +                  newVersion + ", which will destroy all old data");          db.execSQL("DROP TABLE IF EXISTS " + SETTINGS_TABLE);         onCreate(db);      }   }  //end helper class } 
like image 182
Donal Rafferty Avatar answered Oct 13 '22 23:10

Donal Rafferty


the way I'm going here is to ship a prepopulated database in the assets folder. You can drop in files there and use them as-they-are. Beware, however, that there is a size limit of 1MB, so maybe you'll have to split files, or compress them.

Compression is quite handy and well supported by the os itself.

hope this was of any help :-)

like image 43
moritz Avatar answered Oct 14 '22 01:10

moritz