문제

I am trying to develop an app for importing csv in SQLite. I have refer to several samples in order to build my code as follow:

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

            }
        }
    });

However, I have found an error when the button was clicked. Here are the error messages from the log.

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

I have no clues on why the array index was out of bound. I have checked that the csv data can be successfully break into sub strings. Can anyone give some ideas on my coding???

도움이 되었습니까?

해결책

well, it seems that your .split returns 7 tokens. Your str[] is zero-indexed. how about you try doing

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

make it run from str[0] to str[6]

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top