gpt4 book ai didi

android - sqlite从 Assets 中的文本文件批量插入

转载 作者:太空狗 更新时间:2023-10-29 13:29:03 26 4
gpt4 key购买 nike

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

我无法理解该怎么做。

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

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

请告诉我该怎么做。

最佳答案

在 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());

关于android - sqlite从 Assets 中的文本文件批量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18100762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com