08-21 14:29:55.200 25690-25690/? E/uh: (ToDoFragment.java:181) - {}java.sql.SQLException: Getting a writable database from helper so@e781a54 failed
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:27) ~[na:0.0]
at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:70) ~[na:0.0]
at com.j256.ormlite.android.AndroidConnectionSource.getReadOnlyConnection(AndroidConnectionSource.java:55) ~[na:0.0]
at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:243) ~[na:0.0]
at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:197) ~[na:0.0]
at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:277) ~[na:0.0]
at com.j256.ormlite.stmt.QueryBuilder.query(QueryBuilder.java:366) ~[na:0.0]
at com.j256.ormlite.stmt.Where.query(Where.java:480) ~[na:0.0]
at uh.h(ToDoFragment.java:172) ~[na:na]
at ui.a(ToDoFragment_.java:23) ~[na:na]
at ui$13.run(ToDoFragment_.java:157) ~[na:na]
at android.os.Handler.handleCallback(Handler.java:739) ~[na:0.0]
at android.os.Handler.dispatchMessage(Handler.java:95) ~[na:0.0]
at android.os.Looper.loop(Looper.java:135) ~[na:0.0]
at android.app.ActivityThread.main(ActivityThread.java:5254) ~[na:0.0]
at java.lang.reflect.Method.invoke(Native Method) ~[na:0.0]
at java.lang.reflect.Method.invoke(Method.java:372) ~[na:0.0]
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) ~[na:0.0]
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) ~[na:0.0]
Caused by: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method) ~[na:0.0]
at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734) ~[na:0.0]
at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754) ~[na:0.0]
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64) ~[na:0.0]
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1676) ~[na:0.0]
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605) ~[na:0.0]
at android.database.sqlite.SQLiteDatabase.setVersion(SQLiteDatabase.java:873) ~[na:0.0]
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:259) ~[na:0.0]
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) ~[na:0.0]
at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:68) ~[na:0.0]
... 17 common frames omitted
对其进行修改了一下代码,即修复了上述bug!
/* 判断该Dao对应的数据库表是否已经存在,不存在的话创建数据库表 */
private void createTableIfNotExists(Dao, ?> dao, String Prefix, String Suffix) throws SQLException {
logger.error("dao==="+(dao==null?true:false));
- // logger.error("!dao.isTableExists()==="+(!dao.isTableExists()));
- if (false) {//!dao.isTableExists()
+ logger.error("!dao.isTableExists()==="+(!dao.isTableExists()));
+ if (!dao.isTableExists()) {
try {
- createSingleTable(context, SQL_FILE, Prefix, Suffix);//SQL_FILE 数据库文件
+ createSingleTable(context, SQL_FILE, Prefix, Suffix);
logger.error("createSingleTable");
} catch (IOException e) {
logger.error("", e);
@@ -460,7 +460,7 @@ public class PDASqliteOpenHelper extends OrmLiteSqliteOpenHelper {
while ((byteRead = in.read(buffer)) != -1) {
outputStream.write(buffer, 0, byteRead);
}
- String sql = new String(outputStream.toByteArray(), UTF_8.name());
+ String sql = new String(outputStream.toByteArray(), Charset.forName("UTF-8"));
for (String line : sql.split(";(\\s)*[\n\r]")) {
line = line.trim();
boolean iscreateTableSql = line.contains(createTableSql);