gpt4 book ai didi

android - 在读取 sqlite 数据库时遇到问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:57 27 4
gpt4 key购买 nike

我在外部驱动器中使用单例方法创建了一个数据库,当我为 DatabaseHelper 创建一个对象以访问某些方法(例如 checkMISSetting)时,我遇到了问题。

我还在下面附上了我的代码 fragment :

我在其中创建数据库的处理程序类:

private DatabaseHandler(Context context) {

super(context, context.getExternalFilesDir(null).getAbsolutePath()
+ "/" + "Android/MobInvSuit" + "/" + DATABASE_NAME, null,
DATABASE_VERSION);

}

public static synchronized DatabaseHandler getInstance(Context context) {
if (sInstance == null) {
sInstance = new DatabaseHandler(context.getApplicationContext());
}
return sInstance;
}

调用Handler方法

    DatabaseHandler handler=DatabaseHandler.getInstance(this);

boolean check = handler.checkMISSetting();//Line number 129

handler.close();

我遇到错误的方法

public boolean checkMISSetting() {
boolean flag = false;
SQLiteDatabase db = getReadableDatabase();
String q = "SELECT COUNT(*) FROM " + DatabaseHandler.TABLE_SETTINGS;

int numRows = (int) DatabaseUtils.longForQuery(db, q, null);

if (numRows > 0) {
flag = true;
return flag;
} else {
return flag;
}

}

在这种方法中,我在执行附加在这里的 getReadableDatabase() 时遇到问题:

06-21 09:44:03.932: E/SQLiteDatabase(1685): Failed to open database '/storage/emulated/0/Android/data/com.example.mobinventorysuit/files/Android/MobInvSuit/mis.db'.
06-21 09:44:03.932: E/SQLiteDatabase(1685): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:235)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at com.mis.database.DatabaseHandler.checkMISSetting(DatabaseHandler.java:2912)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at com.mis.common.MainActivity.onCreate(MainActivity.java:129)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.app.Activity.performCreate(Activity.java:5231)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.os.Handler.dispatchMessage(Handler.java:102)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.os.Looper.loop(Looper.java:136)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at android.app.ActivityThread.main(ActivityThread.java:5001)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at java.lang.reflect.Method.invoke(Method.java:515)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-21 09:44:03.932: E/SQLiteDatabase(1685): at dalvik.system.NativeStart.main(Native Method)

最佳答案

试试这个

替换 DatabaseHandler 类的构造函数

private DatabaseHandler(Context context) {

super(context,Environment.getExternalStorageDirectory()+ "/" + "Android/MobInvSuit" + "/" + DATABASE_NAME, null,DATABASE_VERSION);

}

关于android - 在读取 sqlite 数据库时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37946232/

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