gpt4 book ai didi

android - 无法打开数据库

转载 作者:行者123 更新时间:2023-12-03 18:11:57 26 4
gpt4 key购买 nike

到目前为止,我的应用程序运行良好,但在某些设备上出现错误(例如 Android 4.1 或 4.3)

 android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:235)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)

在 DBManager 类中,我有这个:
public class DBManager {
public DBHelper helper;
private SQLiteDatabase db;
private Cursor c;

public DBManager(Context context) {
helper = new DBHelper(context);
db = helper.getReadableDatabase();

// db = helper.getWritableDatabase();
}

在 DBHelper 中:
 public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = Environment.getExternalStorageDirectory() + "/database/database.db3";
private static final int DATABASE_VERSION = 3;

public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_SQL);

}

list 等没问题,但我不明白为什么在某些设备上会出现 SQLiteCantOpenDatabaseException 。我希望有人可以帮助我解决这个问题?提前感谢

最佳答案

您正在设置 DATABASE_NAME指向以 Environment.getExternalStorageDirectory() 返回的目录为根的路径.

引用 getExternalStorageDirectory() 的文档:

This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened.



如果你真的不需要指定数据库文件的位置,你应该只指定文件名(例如,“MyDB”),Android 框架会为你确定一个合适的路径。

[更新]

通常,数据库(可能包含非常重要的数据,甚至可能包含敏感的用户信息)不会存储在外部存储中,因为:
  • 其他应用程序可以访问该文件,并且
  • 该文件可能会被用户意外损坏/删除。

  • 如果要创建数据库的备份副本(并将其放在外部存储中),请参阅 this answer .

    关于android - 无法打开数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34937277/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com