android - sqlite bulk insertion from text file in assets -


i facing problem in case while having bulk of insert queries in text file (stored in assets), want execute queries while installation of app after creation of tables.

i unable understand how this.

i have following code, executes only first one of queries.

      @override       public void oncreate(sqlitedatabase db) {       //create table queries       // inserting 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, need execute queries 1 one.

update

using code

stringbuilder sb = new stringbuilder();

try {             inputstream = 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 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):    @    android.database.sqlite.sqlitedatabase.native_execsql(native method)   08-07 16:33:06.217: e/vista_database(8115):    @   android.database.sqlite.sqlitedatabase.execsql(sqlitedatabase.java:1836) 08-07 16:33:06.217: e/vista_database(8115):     @ com.vista.helper.datahelper$openhelper.oncreate(datahelper.java:1140) 08-07 16:33:06.217: e/vista_database(8115):     @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:106) 08-07 16:33:06.217: e/vista_database(8115):     @ com.vista.helper.datahelper.<init>(datahelper.java:105) 08-07 16:33:06.217: e/vista_database(8115):     @ com.bus_service.vista_bus_service.oncreate(vista_bus_service.java:64) 08-07 16:33:06.217: e/vista_database(8115):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 08-07 16:33:06.217: e/vista_database(8115):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2628) 08-07 16:33:06.217: e/vista_database(8115):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2680) 08-07 16:33:06.217: e/vista_database(8115):     @ android.app.activitythread.access$2300(activitythread.java:125) 08-07 16:33:06.217: e/vista_database(8115):     @ android.app.activitythread$h.handlemessage(activitythread.java:2033) 08-07 16:33:06.217: e/vista_database(8115):     @ android.os.handler.dispatchmessage(handler.java:99) 08-07 16:33:06.217: e/vista_database(8115):     @ android.os.looper.loop(looper.java:123) 08-07 16:33:06.217: e/vista_database(8115):     @ android.app.activitythread.main(activitythread.java:4628) 08-07 16:33:06.217: e/vista_database(8115):     @ java.lang.reflect.method.invokenative(native method) 08-07 16:33:06.217: e/vista_database(8115):     @ java.lang.reflect.method.invoke(method.java:521) 08-07 16:33:06.217: e/vista_database(8115):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:879) 08-07 16:33:06.217: e/vista_database(8115):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:637) 08-07 16:33:06.217: e/vista_database(8115):     @ dalvik.system.nativestart.main(native method) 

please let me know how that.

its not possible sqlitedatabse methods in android have repeat loop

may helpful you.

batch operations

try read text file give correct data

bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream();     stringbuffer sb = new stringbuffer();     inputstream inputstream = null;      int i;     try {         inputstream = getapplicationcontext().getassets()                 .open("readme.txt");         = inputstream.read();         while (i != -1) {             bytearrayoutputstream.write(i);             = inputstream.read();         }         inputstream.close();     } catch (ioexception e) {         e.printstacktrace();     }      system.out.println(bytearrayoutputstream.tostring()); 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -