将csv文件导入SQLite数据库

时间:2014-05-07 01:54:13

标签: android sqlite import fatal-error

我正在尝试开发一个用于在SQLite中导入csv的应用程序。我已经参考了几个示例来构建我的代码,如下所示:

Button button_import_csv = (Button) findViewById(R.id.button_import);
    button_import_csv.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){


            DatabaseHelper helper = new DatabaseHelper(getApplicationContext());
            SQLiteDatabase db = helper.getWritableDatabase();


            try{
                FileReader file = new FileReader("/sdcard/downloadedfolder/A1/sales_order.csv");
                BufferedReader buffer = new BufferedReader(file);
                ContentValues contentValues=new ContentValues();
                String line = "";
                String tableName ="sales_order";

                db.beginTransaction();
                while ((line = buffer.readLine()) != null) {
                    String[] str = line.split("\t");


                    contentValues.put("order_date", str[1]);
                    contentValues.put("cust_code", str[2]);
                    contentValues.put("customer_ref", str[3]);
                    contentValues.put("line_no", str[4]);
                    contentValues.put("item_code", str[5]);
                    contentValues.put("tran_code", str[6]);
                    contentValues.put("order_qty", str[7]);
                    db.insert(tableName, null, contentValues);

                }
                db.setTransactionSuccessful();
                db.endTransaction();
            }catch (IOException e){

            }
        }
    });

但是,单击按钮时发现错误。以下是日志中的错误消息。

05-07 09:41:21.478: D/AndroidRuntime(20789): Shutting down VM
05-07 09:41:21.478: W/dalvikvm(20789): threadid=1: thread exiting with uncaught exception (group=0x419baba8)
05-07 09:41:21.518: E/AndroidRuntime(20789): FATAL EXCEPTION: main
05-07 09:41:21.518: E/AndroidRuntime(20789): Process: test.andftpclient, PID: 20789
05-07 09:41:21.518: E/AndroidRuntime(20789): java.lang.ArrayIndexOutOfBoundsException: length=7; index=7
05-07 09:41:21.518: E/AndroidRuntime(20789):    at test.andftpclient.MainActivity$2.onClick(MainActivity.java:116)
05-07 09:41:21.518: E/AndroidRuntime(20789):    at android.view.View.performClick(View.java:4438)
05-07 09:41:21.518: E/AndroidRuntime(20789):    at android.view.View$PerformClick.run(View.java:18422)
05-07 09:41:21.518: E/AndroidRuntime(20789):    at android.os.Handler.handleCallback(Handler.java:733)
05-07 09:41:21.518: E/AndroidRuntime(20789):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-07 09:41:21.518: E/AndroidRuntime(20789):    at android.os.Looper.loop(Looper.java:136)
05-07 09:41:21.518: E/AndroidRuntime(20789):    at android.app.ActivityThread.main(ActivityThread.java:5017)
05-07 09:41:21.518: E/AndroidRuntime(20789):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 09:41:21.518: E/AndroidRuntime(20789):    at java.lang.reflect.Method.invoke(Method.java:515)
05-07 09:41:21.518: E/AndroidRuntime(20789):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-07 09:41:21.518: E/AndroidRuntime(20789):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-07 09:41:21.518: E/AndroidRuntime(20789):    at dalvik.system.NativeStart.main(Native Method)
05-07 09:41:38.678: I/Process(20789): Sending signal. PID: 20789 SIG: 9

我没有关于为什么数组索引超出限制的线索。我已经检查过csv数据可以成功地分解成子字符串。任何人都可以对我的编码提出一些想法吗?

1 个答案:

答案 0 :(得分:0)

好吧,似乎你的.split会返回7个令牌。你的str []是零索引的。你怎么样尝试

contentValues.put("order_date", str[0]);
                contentValues.put("cust_code", str[1]);
                contentValues.put("customer_ref", str[2]);
                contentValues.put("line_no", str[3]);
                contentValues.put("item_code", str[4]);
                contentValues.put("tran_code", str[5]);
                contentValues.put("order_qty", str[6]);
                db.insert(tableName, null, contentValues);

让它从str [0]运行到str [6]