Android SQLiteOpenHelper无法打开数据库文件

我知道这个话题是以前讨论过的。 但我不能find解决我的问题,所以我会再问一次。 我有问题创build数据库。 我复制SQLiteOpenHelper的实现:

public class DBCreator extends SQLiteOpenHelper { private static final String DATABASE_NAME = "servision.db"; private static final int DATABASE_VERSION = 1; private static final String INFO_TABLE ="create table INFO(key text,value text);"; public static final String GATEWAYS_TABLE = "GATEWAYS"; // Table name public static final String TABLE = "events"; // Columns public static final String TIME = "time"; public static final String TITLE = "title"; public DBCreator(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { try { String gatewaytablescript ="create table" +GATEWAYS_TABLE+"(" + "id int primary key, " + "host text not null," + "port int not null," + "username text," + "password text,"+ "useproxy int," + "proxyHost text," + "proxyPort port," + "desc text," + "secondaryip text," + "secondaryport int," + "timezone int," + "encryption int," + "customkey text" + ");"; Log.d("DBCreator", "Creating " + GATEWAYS_TABLE + "table"); db.execSQL(gatewaytablescript); String infotablescript ="create table " + INFO_TABLE + "(key text,value text);"; Log.d("DBCreator", "Creating " + INFO_TABLE + "table"); db.execSQL(infotablescript); } catch(SQLException ex) { Log.d("DBCreator", "onCreate exception " +ex.getMessage()); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.d("EventsData", "onUpgrade"); if (oldVersion >= newVersion) return; } } 

我创build它从这样的主要活动onCreate方法。

 DBCreator db =new DBCreator(this); db.getReadableDatabase(); 

在logcat我收到以下信息:

 06-01 17:31:15.523: INFO/global(2470): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required. 06-01 17:31:15.652: INFO/Database(4160): sqlite returned: error code = 14, msg = cannot open file at source line 25467 06-01 17:31:15.652: ERROR/Database(4160): sqlite3_open_v2("/data/data/com.servision.svclient/databases/servision.db", &handle, 6, NULL) failed 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): Couldn't open servision.db for writing (will try read-only): 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): android.database.sqlite.SQLiteException: unable to open database file 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1921) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:883) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:960) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:953) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:602) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at com.servision.svclient.Main.onCreate(Main.java:52) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.os.Handler.dispatchMessage(Handler.java:99) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.os.Looper.loop(Looper.java:123) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at android.app.ActivityThread.main(ActivityThread.java:4627) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at java.lang.reflect.Method.invokeNative(Native Method) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at java.lang.reflect.Method.invoke(Method.java:521) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 06-01 17:31:15.722: ERROR/SQLiteOpenHelper(4160): at dalvik.system.NativeStart.main(Native Method) 06-01 17:31:15.738: INFO/Database(4160): sqlite returned: error code = 14, msg = cannot open file at source line 25467 06-01 17:31:15.738: ERROR/Database(4160): sqlite3_open_v2("/data/data/com.servision.svclient/databases/servision.db", &handle, 1, NULL) failed 

DBCreator的onCreate永远不会被调用。 我在Eclipse上以debugging模式在Galaxy S上运行程序。 我是否缺less一些权限? 可能上下文不好还是什么?

Solutions Collecting From Web of "Android SQLiteOpenHelper无法打开数据库文件"

相对于sqlite returned: error code = 14, msg = cannot open file at source line 25467sqlite returned: error code = 14, msg = cannot open file at source line 25467 sqlite3_open_v2("/data/data/com.servision.svclient/databases/servision.db", &handle, 1, NULL) failed错误,这似乎是没有数据库时,Android试图创build一个。

一些在这里阅读

http://androidblogger.blogspot.com/2011/02/instable-android-and-unable-to-open.html

http://www.itsalif.info/content/check-if-database-exist-android-sqlite3openv2-failed

http://code.google.com/p/android/issues/detail?id=949

检查你的清单添加权限

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

当我得到这个错误,我用这个代码为我的构造函数

 public DBClass(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); context.openOrCreateDatabase(DATABASE_NAME, context.MODE_PRIVATE, null); } 

我的应用程序出现问题,在重新安装应用程序之后,使用旧的数据库更改我的数据库文件时,出现了这种错误。 我想问题是,旧的数据库文件的所有者是不是重新安装的应用程序的文件的所有者相同。 所以它解决了赋予新文件完整的权限(777),所以也可以被其他用户读取。

最后,确保您没有文件权限问题。

我有一个类似的问题,这是我如何解决它:

https://stackoverflow.com/a/22743794/1947094

总结一下:

 adb shell cd /data/data/YOUR.APP.PACKAGE/databases/ ls 

这给你一个你的应用程序使用的数据库的列表。

 cp your_database.db your_database.db_backup rm your_database.db 

已经在这个链接提出的bug

http://code.google.com/p/android/issues/detail?id=949

使用下面的API …

context.openOrCreateDatabase(“sample.db”,MODE_PRIVATE,null);

试试这个链接

http://www.itsalif.info/content/check-if-database-exist-android-sqlite3openv2-failed