When I get the data from the server, I insert the data into the my own content provider, but it always error. This is the log
Caused by: java.lang.IllegalStateException: database not open
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1585)
at com.weiboa.data.StatusProvider.insert(StatusProvider.java:107
at android.content.ContentProvider$Transport.insert(ContentProvider.java:210)
at android.content.ContentResolver.insert(ContentResolver.java:606)
at com.weiboa.util.WeiboUserUtil.insertTweet(WeiboUserUtil.java:121)
and this is my insert code in my ContentProvider
:
SQLiteDatabase db = dbHelper.getWritableDatabase();
try{
long id = db.insertWithOnConflict(TABLE, null, values,SQLiteDatabase.CONFLICT_REPLACE);
if(id == -1){
throw new RuntimeException(String.format("%s : Failed to insert [%s] to [%s] for unknow reason,", TAG, values, uri));
}else{
return ContentUris.withAppendedId(uri, id);
}
}finally{
db.close();
}
And i find the error message in the android source code, it will check the SQLiteDatabase.isOpen(), and in the function getWritableDatabase, it always checked the database is open?
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
return mDatabase; // The database is already open for business
}
So I don't know the real reason to caused this error why the database is not opened.