- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试学习 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/
我注意到当我右键单击该项目并选择“作为 Android 应用程序运行”时,openOrCreateDatabase 只能在模拟器中使用,而不能在手机中使用。我已经检查了 DDMS,数据库是通过模拟器成
我有我的 DAO 类,当我调用我的类 conexao 时,该方法返回 null,但为什么? public void criarBanco() { Conexao conexao = n
我需要帮助。我有一个带有自定义适配器的 ListView,在这个 ListView 中我有一个 CheckBox 并且在 onClick 方法中应该更新我的数据库中的一个表。我的 CheckBox 的
我在打开数据库时在我的 EhActivity 类中遇到 NullPointerException public class EHActivity extends Activity { ArrayLi
我有以下 DBAdapter(这不是全部,但它是相关代码)。 private final String DATABASE_NAME = "seller"; private final String D
知道如何更新使用 openorcreatedatabase 创建的 sqlite 数据库。问题是该应用程序已经在市场上发布。新版本的应用程序向数据库添加了字段。 我试过这个方法 package com
我正在尝试访问数组适配器内的数据库以从列表中删除项目。但代码无法解析 openOrCreateDatabase 方法或符号 MODE_PRIVATE。 我的代码: public class worko
我创建了一个带有服务的程序 - 该服务是“START_STICKY”。因此,当我关闭程序(我正在使用 Advanced Task Killer 来终止程序)时,服务会自动重新启动,但这会出现问题,因为
当我打电话 myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null); 数据库文件是在哪里创建的? 我到处都找不到。 o
Android 在两个不同的类中有 openOrCreateDatabase:SQLiteDatabase和 Context .请问它们有什么区别? 与此同时,我正在使用单例类来维护我的数据库。如果我
我正在尝试按如下方式打开数据库: SQLiteDatabase myDatabase; myDatabase = openOrCreateDatabase("sudoku.db", Context.M
我正在制作一个使用数据库的新 Android 应用程序。我使用类似的代码来创建和复制数据库,但由于某种原因它不起作用。想不通,求助日志猫: 07-03 21:38:54.260: E/AndroidR
我正在尝试学习 sqlite for android。我创建了一个 Helper 类,它应该打开我的数据库并执行任何帮助类所做的事情。 我的问题是由于这个错误我什至无法打开数据库: android.d
我收到此日志警告消息(在运行 stock 4.4 的 Nexus 4 上): Failed to chmod(/storage/emulated/0/Android/data/com.myapp/my
我是一名优秀的程序员,十分优秀!