0

I just new in android development and trying to create a database using right way for it.

What I have: Contract class, that stores create table scripts (On finish I will have about 10-12 tables).

Whats the Question: It's exist some good way (good practice) to import data from files to DB right after table was created. Maybe some patterns or examples?

Alex
  • 115
  • 1
  • 5

1 Answers1

0

You can store some file with database queries in raw folder and then just read it and insert everything into database for example:

@Override
public void onCreate(SQLiteDatabase db) {
    InputStream inputStream = null;
    try {
        inputStream = context.getResources().openRawResource(R.raw.db_initial_import);
        final String queries = IOUtils.toString(inputStream);
        db.execSQL(queries);
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}
Orest
  • 6,548
  • 10
  • 54
  • 84