Reputation: 721
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.
Upvotes: 1
Views: 1443
Reputation: 9284
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.
Upvotes: 4