I am facing a problem in case while I am having a bulk of insert queries in my text file (stored in assets), I do want to execute all those queries while installation of app after creation of tables.
I am unable to understand how to do this.
I have the following code, that executes only the first one of all queries.
@Override
public void onCreate(SQLiteDatabase db) {
//create table queries
// inserting from the text file in assets
InputStream in_s = cntx.getAssets().open("VistaLog.txt");
byte[] b = new byte[in_s.available()];
in_s.read(b);
String sql = new String(b);
db.execSQL(sql);
}
But, I do need to execute all queries one by one.
Update
Using this code
StringBuilder sb = new StringBuilder();
try {
InputStream is = ctx.getAssets().open("VistaLog2.txt");
Reader reader = new InputStreamReader(is);
char[] chars = new char[8192];
for(int len; (len = reader.read(chars)) > 0;) {
// process chars.
sb.append((char)len);
}
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("chunkfile", "error here: "+e.getMessage());
}
String[] queries = sb.toString().split(";");
for(String query : queries){
Log.d("vista_database", "query: "+query);
db.execSQL(query);
}
Gives me the following error.
08-07 16:33:06.187: E/Database(8115): Failure 1 (near
"
ᆱ": syntax error) on 0x2a2ba8 when preparing
'
ᆱ'.
08-07 16:33:06.217: E/vista_database(8115): android.database.sqlite.SQLiteException:
near " ᆱ":
syntax error: ᆱ
08-07 16:33:06.217: E/vista_database(8115): at
android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
08-07 16:33:06.217: E/vista_database(8115): at
android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1836)
08-07 16:33:06.217: E/vista_database(8115): at com.Vista.Helper.DataHelper$OpenHelper.onCreate(DataHelper.java:1140)
08-07 16:33:06.217: E/vista_database(8115): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)
08-07 16:33:06.217: E/vista_database(8115): at com.Vista.Helper.DataHelper.<init>(DataHelper.java:105)
08-07 16:33:06.217: E/vista_database(8115): at com.bus_service.Vista_bus_service.onCreate(Vista_bus_service.java:64)
08-07 16:33:06.217: E/vista_database(8115): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-07 16:33:06.217: E/vista_database(8115): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
08-07 16:33:06.217: E/vista_database(8115): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2680)
08-07 16:33:06.217: E/vista_database(8115): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-07 16:33:06.217: E/vista_database(8115): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-07 16:33:06.217: E/vista_database(8115): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 16:33:06.217: E/vista_database(8115): at android.os.Looper.loop(Looper.java:123)
08-07 16:33:06.217: E/vista_database(8115): at android.app.ActivityThread.main(ActivityThread.java:4628)
08-07 16:33:06.217: E/vista_database(8115): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 16:33:06.217: E/vista_database(8115): at java.lang.reflect.Method.invoke(Method.java:521)
08-07 16:33:06.217: E/vista_database(8115): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:879)
08-07 16:33:06.217: E/vista_database(8115): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:637)
08-07 16:33:06.217: E/vista_database(8115): at dalvik.system.NativeStart.main(Native Method)
Please let me know how to do that.