导入多个csv文件后,Android数据库被锁定

时间:2014-07-11 03:02:11

标签: android database sqlite

我正在尝试使用for循环将多个csv文件导入Android 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();
            int returnvalue;
            long ret;
            Log.d("ice","database opened");
            returnvalue=db.delete(tableName[0], null, null );
            Log.d("ice","Returnvalue of deleting table0:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[1], null, null );
            Log.d("ice","Returnvalue of deleting table1:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[2], null, null );
            Log.d("ice","Returnvalue of deleting table2:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[3], null, null );
            Log.d("ice","Returnvalue of deleting table3:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[4], null, null );
            Log.d("ice","Returnvalue of deleting table4:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[5], null, null );
            Log.d("ice","Returnvalue of deleting table5:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[6], null, null );
            Log.d("ice","Returnvalue of deleting table6:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[7], null, null );
            Log.d("ice","Returnvalue of deleting table7:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[8], null, null );
            Log.d("ice","Returnvalue of deleting table8:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[9], null, null );
            Log.d("ice","Returnvalue of deleting table9:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[10], null, null );
            Log.d("ice","Returnvalue of deleting table10:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[11], null, null );
            Log.d("ice","Returnvalue of deleting table11:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[12], null, null );
            Log.d("ice","Returnvalue of deleting table12:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[13], null, null );
            Log.d("ice","Returnvalue of deleting table13:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[14], null, null );
            Log.d("ice","Returnvalue of deleting table14:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[15], null, null );
            Log.d("ice","Returnvalue of deleting table15:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[16], null, null );
            Log.d("ice","Returnvalue of deleting table16:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[17], null, null );
            Log.d("ice","Returnvalue of deleting table17:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[18], null, null );
            Log.d("ice","Returnvalue of deleting table18:" + Integer.toString(returnvalue));
            returnvalue=db.delete(tableName[19], null, null );
            Log.d("ice","Returnvalue of deleting table19:" + Integer.toString(returnvalue));

            Log.d("ice","old data existed in database was all cleared");
            try{
                File csvpath = new File("/sdcard/downloadedfolder/"+route_folder);
                Log.d("ice","Path of csv folder get in modile");
                File[] csvfile = csvpath.listFiles();
                //get all csv files' name and store it as array
                Log.d("ice","Name of csv was get and change into string");
                //Put all name of csv into a string array
                /*FileReader file = new FileReader("/sdcard/downloadedfolder/A1/adv_sales_order.csv");
                BufferedReader buffer = new BufferedReader(file);*/
                for(int j=0;j<20;j++){
                    FileReader file = new FileReader(csvfile[j]);
                    Log.d("ice","Current file reading");
                    Log.d("ice","csvfile"+Integer.toString(j)+": "+file.toString());
                    BufferedReader buffer = new BufferedReader(file);
                    ContentValues contentValues=new ContentValues();
                    String line = buffer.readLine();            //read first line to get the column
                    Log.d("ice","Column of "+file.toString()+": "+ line);
                    String[] cols = line.split("\t");           
                    db.beginTransaction();

                    while ((line = buffer.readLine()) != null) {
                        Log.d("ice","Read next record");
                        Log.d("ice","Line"+ Integer.toString(j)+": "+ line);
                        //read every single line of record in csv
                        String[] str = line.split("\t");
                        for (int i = 0; i < cols.length; i++) {
                            contentValues.put(cols[i], str[i]);
                            Log.d("ice",cols[i] + "= " + str[i]);
                        }
                        ret = db.insert(tableName[j], null, contentValues);
                        Log.d("ice","Import result for " + tableName[j] +"= ");
                        Log.d("ice", Long.toString(ret));

                    }
                    buffer.close();
                }

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

            }

            helper.close();
        }
    });

每当调用数据库的任何函数时,这都是错误日志:

  

07-11 10:39:52.076:E / SQLiteDatabase(25198):无法打开数据库'/data/data/com.iceapp/databases/icedb.db'。   07-11 10:39:52.076:E / SQLiteDatabase(25198):android.database.sqlite.SQLiteDatabaseLockedException:数据库被锁定(代码5):,编译时:PRAGMA journal_mode

我也在logcat中找到了相关的消息

  

/data/data/com.iceapp/databases/icedb.db的连接池已关闭,但仍有1个连接正在使用中。当他们被释放回游泳池时,他们将被关闭。

我没有关于数据库关闭和使用哪个连接的原因的线索。谁能给我一些帮助?谢谢。

1 个答案:

答案 0 :(得分:0)

  1. 确保SQLiteOpenHelper是Singleton,这样可以防止数据库被锁定。

  2. 始终调用db.yieldIfContendedSafely()。在做交易时。这样可以防止数据库死锁。

    db.beginTransaction();

    for(..){

    ...//insert
    
     db.yieldIfContendedSafely();
    

    }

    db.setTransactionSuccessful();

    db.endTransaction();