我试图DatabaseInterface
通过打开它MyApplication extends Application
并基本上从不关闭它来改变我的实现(我做了一些研究,谷歌工程师推荐它,请参阅链接的问题Best place to close database connection)。我所做的修改非常小,但现在系统在打开数据库时向我抛出以下错误:
E/SQLiteDatabase(15690): android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
这是相关代码
我的应用程序.java
@Override
public void onCreate() {
super.onCreate();
try {
DatabaseInterface.open();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
数据库接口.java
public class DatabaseInterface extends SQLiteOpenHelper {
private final static String DB_PATH = Constants.DB_PATH;
private final static String DB_NAME = Constants.DB_NAME;
private static SQLiteDatabase mDatabase;
private Context mContext;
private static DatabaseInterface mInstance;
private DatabaseInterface(Context context) {
super(context, DB_NAME, null, 1);
this.mContext = context;
}
public static DatabaseInterface getInstance(Context context) {
if (mInstance == null) {
mInstance = new DatabaseInterface(context);
}
return mInstance;
}
public static void open() throws SQLException, IOException {
//Open the database
String path = DB_PATH + DB_NAME;
mDatabase = SQLiteDatabase.openOrCreateDatabase(new File(path), null);
}
}