Android : Android database was locked after importing multiple csv files

on Thursday, July 10, 2014


I am trying to use a for loop to import multiple csv files into Android sqlite. However, when the app try to import the csv content into database, the database will be locked and all other functions involved with database will fail.


Here is my code Import function:



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


This is the error log whenever any function calling the database:



07-11 10:39:52.076: E/SQLiteDatabase(25198): Failed to open database '/data/data/com.iceapp/databases/icedb.db'. 07-11 10:39:52.076: E/SQLiteDatabase(25198): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode



I also have found a related message in logcat



The connection pool for /data/data/com.iceapp/databases/icedb.db has been closed but there are still 1 connections in use. They will be closed as they are released back to the pool.



I have no clues on why the database was closed and which connection is in used. Can anyone give me some help?? Thank you.


0 comments:

Post a Comment