Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法在特定设备上为ReadWirte打开数据库_Android_Sqlite - Fatal编程技术网

Android 无法在特定设备上为ReadWirte打开数据库

Android 无法在特定设备上为ReadWirte打开数据库,android,sqlite,Android,Sqlite,我只是尝试以读写模式打开数据库。我得到以下错误:“非错误(代码0):无法以读/写模式打开数据库。”以相同的方式打开数据库时,只读是有效的。在我的3台设备上,在READWRITE中打开并没有问题,但有2位用户报告了以下错误 数据库存在于用户文件系统上。我用file.exists()->ok检查这个。 db文件在用户设备上是可读写的。我用file.canWrite()->ok检查这个 数据库文件存储在以下位置: 存储路径:/mnt/extSdCard/mypath/mydb.db 2014年3月9日

我只是尝试以读写模式打开数据库。我得到以下错误:“非错误(代码0):无法以读/写模式打开数据库。”以相同的方式打开数据库时,只读是有效的。在我的3台设备上,在READWRITE中打开并没有问题,但有2位用户报告了以下错误

数据库存在于用户文件系统上。我用file.exists()->ok检查这个。 db文件在用户设备上是可读写的。我用file.canWrite()->ok检查这个

数据库文件存储在以下位置:

存储路径:/mnt/extSdCard/mypath/mydb.db

2014年3月9日的新信息:这似乎只是KitKat 4.4.2中的一个问题。因为用户已经更新到4.4.2,所以他们遇到了这个问题

我的代码:

public void onCreate(Bundle savedInstanceState) {


connectWriter(); // throws exception below.

public void connectWriter() {
        chronica_connection_readwrite = SQLiteDatabase.openDatabase("MyPath", null, SQLiteDatabase.OPEN_READWRITE);
        //chronica_connection_read.enableWriteAheadLogging();
    }
例外报告:

java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
at android.app.ActivityThread.access$800(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:342)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:232)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:515)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:207)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:891)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:859)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
at myclass.connectWriter(ChronicBrowser.java:14286)
at myclass.LoadModule(ChronicBrowser.java:10792)
at myclass.onCreate(ChronicBrowser.java:761)
at android.app.Activity.performCreate(Activity.java:5389)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)


************ DEVICE INFORMATION ***********
Brand: samsung
Device: hlte
Model: SM-N9005
Id: KOT49H
Product: hltexx

************ FIRMWARE ************
SDK: 19
Release: 4.4.2
Incremental: N9005XXUENB7 

看起来这不是bug,而是“特性”


这真的很让人伤心,但看起来像是真的

我会先说,您应该将SQLite数据库存储在内部存储上,这样您就可以利用ext4日志帮助数据恢复。在内部存储上也有更好的安全性,因为您不必担心恶意应用程序添加触发器等最终会在您的进程中运行

总之,从KitKat开始,您可以通过新的公共API
Context.getExternalFilesDirs()
Context.getExternalCacheDirs()
写入辅助存储设备。这些方法也可以在support-v4库的
ContextCompat
上找到

另外请注意,如果您只对使用
上下文返回的目录感兴趣,则不再需要
读取
写入外部存储
权限。接下来,您将始终拥有对这些目录的读/写访问权限,而不需要额外的权限

应用程序还可以在解除权限请求后继续在旧设备上工作,如下所示:

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


为什么不使用
SQLiteOpenHelper
?在不同模式下打开数据库时,您不必直接处理数据库。我真的希望使用我的连接。我的数据库不是由应用程序创建的,我没有版本控制。我的数据库将由我的应用程序下载。。我想这应该可以在每个设备上打开读写连接。有什么提示吗?所有设备都运行相同的android版本吗?我建议使用SQLiteOpenHelper,您可以始终在应用程序中维护版本号。我认为这不是不使用SQLiteOpenHelper的好借口。我还从外部来源将db文件下载到我的应用程序中,但我正在使用SQLiteOpenHelper并在我的应用程序中维护该版本。我将尝试它,但根据我的说法,不需要使用SQLiteOpenHelper以读写模式打开数据库。我还喜欢使用“enableWriteAheadLogging”日志记录,我不知道SQLiteOpenHelper是如何支持它的。在打开数据库方面,SQLiteDatabase.openDatabase与SQLiteOpenHelper.getReadableDatabase之间似乎没有什么区别。在升级到4.4.2之后,我能够通过将数据库移动到Galaxy S4上的内部存储来解决此问题。我同意这是悲哀的,因为它现在使用了内部内存,而SD卡上有很多内存。希望有另一种解决方法。那么,使用这些API
Context.getExternalFilesDirs()
,是否可以帮助我们将SQLite db存储在辅助存储中?我认为这些API用于存储与我们的应用程序相关的一些文件,而不是sqlite db。如果我错了,请纠正我。