3

如果我的文本文件(存储在资产中)中有大量插入查询,我会遇到问题,我确实想在创建表后安装应用程序时执行所有这些查询。

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

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

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

请让我知道该怎么做。

4

1 回答 1

2

在android中使用SQlitedatabse方法是不可能的,因为你必须重复循环

可能这对你有帮助。

批处理操作

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

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());
于 2013-08-07T10:28:11.360 回答