从资产中的文本文件中插入sqlite批量

时间:2013-08-07 10:18:24

标签: android text-files android-sqlite bulkinsert android-assets

我在文本文件中存在大量插入查询(存储在资源中)时遇到问题,我确实希望在创建表格后安装app时执行所有这些查询。

我无法理解如何做到这一点。

我有以下代码,只执行所有查询的第一个

      @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);

  }

但是,我确实需要逐个执行所有查询。

更新

使用此代码

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);
        }

给我以下错误。

  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)

请让我知道如何做到这一点。

1 个答案:

答案 0 :(得分:2)

对于Android中的SQlitedatabse方法,这是不可能的,你必须重复循环

可能会对你有所帮助。

batch operations

尝试从文件中读取这样的文本,它将提供正确的数据

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    StringBuffer sb = new StringBuffer();
    InputStream inputStream = null;

    int i;
    try {
        inputStream = getApplicationContext().getAssets()
                .open("readme.txt");
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println(byteArrayOutputStream.toString());