0

我试图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);
    }
}
4

1 回答 1

3

好的,这是因为我忘记添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

到我AndroidManifest.xml的,因为我的数据库驻留在手机的外部存储上。

于 2013-04-03T01:09:09.620 回答