我有一個CSV文件位於我的資產文件夾。我需要我的程序來獲取CSV文件,然後將其數據插入到SQLite中的表中。讀取CSV然後插入數據到SQLITE
這裏是我當前的代碼
FileReader file;
file = new FileReader("com.xylol.o_cha/assets/questions/question_bank.csv");
BufferedReader buffer = new BufferedReader(file);
String line = "";
String tableName ="O_CHA_QUESTIONS";
String columns = "QUESTION_ID, CATEGORY, ANIME TITLE, QUESTION_DESC, CHOICE1, CHOICE2, CHOICE3, CHOICE4, CORRECT_ANSWER, ANSWERED";
String str1 = "INSERT INTO " + tableName + " (" + columns + ") values(";
String str2 = ");";
database.beginTransaction();
while ((line = buffer.readLine()) != null) {
StringBuilder sb = new StringBuilder(str1);
String[] str = line.split(",");
sb.append("'" + str[0] + "',");
sb.append(str[1] + "',");
sb.append(str[2] + "',");
sb.append(str[3] + "'");
sb.append(str[4] + "'");
sb.append(str2);
database.execSQL(sb.toString());
}
database.setTransactionSuccessful();
database.endTransaction();
這裏的問題
09-23 05:47:29.436: W/System.err(7020): java.io.FileNotFoundException: /com.xylol.o_cha/assets/questions/question_bank.csv (No such file or directory)
09-23 05:47:29.465: W/System.err(7020): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
09-23 05:47:29.465: W/System.err(7020): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239)
09-23 05:47:29.473: W/System.err(7020): at java.io.FileInputStream.<init>(FileInputStream.java:88)
09-23 05:47:29.473: W/System.err(7020): at java.io.FileInputStream.<init>(FileInputStream.java:122)
09-23 05:47:29.473: W/System.err(7020): at java.io.FileReader.<init>(FileReader.java:66)
09-23 05:47:29.483: W/System.err(7020): at com.xylol.o_cha.Splash_Screen$1.run(Splash_Screen.java:73)
09-23 05:47:29.483: W/System.err(7020): at android.os.Handler.handleCallback(Handler.java:587)
09-23 05:47:29.493: W/System.err(7020): at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 05:47:29.493: W/System.err(7020): at android.os.Looper.loop(Looper.java:132)
09-23 05:47:29.493: W/System.err(7020): at android.app.ActivityThread.main(ActivityThread.java:4025)
09-23 05:47:29.493: W/System.err(7020): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 05:47:29.493: W/System.err(7020): at java.lang.reflect.Method.invoke(Method.java:491)
09-23 05:47:29.493: W/System.err(7020): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-23 05:47:29.503: W/System.err(7020): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-23 05:47:29.526: W/System.err(7020): at dalvik.system.NativeStart.main(Native Method)
您的問題究竟是什麼?我看到一些遺漏的代碼和一些可以做出的優化,但是如果沒有......一個問題就很難回答問題。 – elbuild
查看我的答案,瞭解如何使用資產文件夾。 – Tugrul