无法以读/写模式打开数据库 [英] Not able to open database in read/write mode
问题描述
我遇到以下错误:
不是错误(代码0):无法以读写模式打开数据库.
not an error (code 0): Could not open the database in read/write mode.
我已添加
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我正在尝试在SD卡中存在的数据库中输入数据,我能够通过"OPEN_READONLY"读取数据库中已存在的数据.使用"OPEN_READWRITE"时出现错误
I am trying to enter data in the database that is present in the SD card, I am able to read the data that is already present in the database through "OPEN_READONLY". I am getting error when using "OPEN_READWRITE"
推荐答案
您的问题不清楚,但我会尽力回答.
Your question is not that clear, but I'll try to answer.
您的数据库很可能是
- 尚不存在,您必须创建它;
- 您的数据库文件是只读的,您必须进行更改(
- Don't exist yet, and you have to create it;
- Your database file is read only, you have to change it (This question might be related).
对于#2,而不是使用 SQLiteOpenHelper#getWritableDatabase
For #2, instead of using SQLiteOpenHelper#getReadableDatabase()
, use SQLiteOpenHelper#getWritableDatabase
如果您的数据库位于外部存储单元上,则还需要检查其他几件事:
If your database is on an external storage unit, you have a few other things to check:
- 当前是否已安装外部存储?如果没有,您将无法访问数据库.
- 是否将其安装为只读?如果是这样,则必须对此进行更改.
- 您检查过路径了吗?正确吗?
问题可能出在任何这些主题上.
The problem might be on any of those topics.
要检查它是否以只读方式安装,请尝试以下操作:
To check if it's mounted as read-only, try the following:
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
来自此处.
有关Environment
类的更多信息,请参阅文档.
For more info on the Environment
class, please refer to the docs.
这篇关于无法以读/写模式打开数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!