从MyApplication打开SQLite DB - 无法以读/写模式打开

时间:2013-04-03 00:55:04

标签: android sqlite

我正在尝试通过在DatabaseInterface中打开MyApplication extends Application来实现更改,并且基本上从不关闭它(我做了一些研究并且Google工程师推荐了它,请参阅链接的问题{{3} })。我所做的修改非常小,但现在系统在打开数据库时会抛出以下错误:

E/SQLiteDatabase(15690): android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.

这是相关代码

MyApplication.java

@Override
public void onCreate() {
    super.onCreate();
    try {
        DatabaseInterface.open();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

DatabaseInterface.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);
    }
}

1 个答案:

答案 0 :(得分:3)

好的,这是我忘记添加

引起的

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

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