1

I can read a text file which is in the assets folder. But now I have a DB creation query in the text file; how can I execute that query in Android? Please don't ask why I want to keep queries in a plain text file.

halfer
  • 19,824
  • 17
  • 99
  • 186
S.Basnagoda
  • 671
  • 2
  • 8
  • 21

1 Answers1

4

You should use a SqliteOpenHelper and override the onCreate method. Inside that method you should read the file from resources and execute it.

See this for how to use an SqliteOpenHelper

Then for reading file from resources just do this (from here):

    @Override
    public void onCreate(SQLiteDatabase database) {
      Resources res = getResources();
      InputStream in_s = res.openRawResource(R.raw.sql);
      byte[] b = new byte[in_s.available()];
      in_s.read(b);
      String sql = new String(b);
      database.execSQL(sql);
    }

Then you also need to implement onUpgrade (since it is marked as abstract) but you don't need to do anything in that method until you decide to change the structure of your database.

Community
  • 1
  • 1
Matt Wolfe
  • 8,924
  • 8
  • 60
  • 77