I have an issue that has got me pretty stumped, and would appreciate some input with. I have a thread in my app which inserts into a database from a csv file. Everything seems fine, but recently Im getting database not open stack traces sent to me from the developer console.
Its happining to a really small portion of my users, maybe 1 in a 100 have this issue, I can run the insert for hours before I finally see the error, and when I do theres no difference than what I did the other 80 times when it worked fine.
Heres the stack trace:
java.lang.IllegalStateException: database not open
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1615)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1515)
at com.ljworkshop.YugiohCompanion.DBAdapterCards.insertCard(DBAdapterCards.java:169)
at com.ljworkshop.YugiohCompanion.home$8.run(home.java:1020)
at java.lang.Thread.run(Thread.java:1027)
Heres the section of code:
public void cardBaseupdate(String file,int amount) {
final String files = file;
final int amountf = amount;
dialog = new ProgressDialog(home.this);
dialog.setCancelable(false);
dialog.setMessage("Populating Database");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
dialog.setMax(amountf);
dialog.show();
Thread background = new Thread (new Runnable() {
public void run() {
InputStream data1 = null;
try {
data1 = getAssets().open(files);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader in = new BufferedReader(new InputStreamReader(data1));
String reader = "";
try {
while ((reader = in.readLine()) != null){
Integer.toString(in.readLine().length()),
String[] RowData = reader.split(",");
String name = RowData[0];
String number = RowData[1];
String series = RowData[2];
String type = RowData[3];
String element = RowData[4];
String level = RowData[5];
String spell = RowData[6];
String att = RowData[7];
String def= RowData[8];
String rules = RowData[9];
String legal = RowData[10];
dbc.open();
progressHandler.sendMessage(progressHandler.obtainMessage());
dbc.insertCard(name,number,series,type,element,level,spell,att,def,rules,legal);
dbc.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
valueSet = true;
}
});
background.start();
I hope when you say Thread, you mean you are using an AsyncTask. The reason for this is because an AsyncTask is optimized for android in threading.
Ideally when you running your database logic, you want to insure that you close your database EVERYTIME by using the finally clause. This will ensure that it is closed each time it is opened during your logic.
Also a thread is not a safe place to be doing database transactions, typically you run your logic (in a thread) and then update the database once you have new data.