gpt4 book ai didi

android - SQLite openOrCreateDatabase 未知错误(代码 14): Could not open database

转载 作者:搜寻专家 更新时间:2023-10-30 19:42:57 26 4
gpt4 key购买 nike

我正在尝试学习 sqlite for android。我创建了一个 Helper 类,它应该打开我的数据库并执行任何帮助类所做的事情。

我的问题是由于这个错误我什至无法打开数据库:

android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

这是我的DatabaseHelper类的相关代码,错误在openOrCreateDatabase这一行:

public class DatabaseHelper<T> implements IDatabaseHelper{
private static final String DATABASE_NAME = "myDatabase.db";
private static String DATABASE_PATH = "";
private static final int DATABASE_VERSION = 1;
private static SQLiteDatabase database;
private static Context ctx;

//constructor
public DatabaseHelper(Context context){
ctx = context;
DATABASE_PATH = ctx.getFilesDir().getPath();
}

//apertura database
public boolean OpenDB() {
//try{
database = SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME, null, null);
return true;
}

都被 try catch 包围了,但问题还是打不开我的数据库。这是堆栈跟踪。

11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: Failed to open database 'myDatabase.db'.
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at
android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:717)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at personal.pier.weightwatcher.Utils.DatabaseHelper.OpenDB(DatabaseHelper.java:85)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at personal.pier.weightwatcher.Activities.SplashScreenActivity.onCreate(SplashScreenActivity.java:22)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.app.Activity.performCreate(Activity.java:5990)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.app.ActivityThread.access$900(ActivityThread.java:154)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.os.Looper.loop(Looper.java:135)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at android.app.ActivityThread.main(ActivityThread.java:5292)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at java.lang.reflect.Method.invoke(Native Method)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at java.lang.reflect.Method.invoke(Method.java:372)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
11-05 17:27:35.410 16641-16641/? D/AndroidRuntime: Shutting down VM
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: FATAL EXCEPTION: main
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: Process: personal.pier.weightwatcher, PID: 16641
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{personal.pier.weightwatcher/personal.pier.weightwatcher.Activities.SplashScreenActivity}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5292)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:717)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at personal.pier.weightwatcher.Utils.DatabaseHelper.OpenDB(DatabaseHelper.java:85)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at personal.pier.weightwatcher.Activities.SplashScreenActivity.onCreate(SplashScreenActivity.java:22)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5990)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5292) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

我真的找不到解决办法。任何帮助将不胜感激。谢谢指教

PS:我没有在 porpouse 上扩展 SQLiteOpenHelper,因为我想对操作进行更多控制,因为我试图对我的所有类只使用一个助手。

最佳答案

调用SQLiteDatabase.openOrCreateDatabase时,第一个参数(String path)应该是文件系统的完整路径,从getFilesDir() 或类似调用

关于android - SQLite openOrCreateDatabase 未知错误(代码 14): Could not open database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33549889/

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