Android打开或创建数据库 [英] Android open or create database

查看:224
本文介绍了Android打开或创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的SD卡上创建数据库。每当我调用SQLiteDatabase.openOrCreateDatabase我得到的错误:

I am trying to create a database on my sd card. Whenever I call SQLiteDatabase.openOrCreateDatabase I get the error:


07-21 13:33:17.587:ERROR / AndroidRuntime通过:
android.database.sqlite.SQLiteException:无法打开数据库文件

07-21 13:33:17.587: ERROR/AndroidRuntime(5541): Caused by: android.database.sqlite.SQLiteException: unable to open database file

有人知道可能是什么原因?下面是我的数据库类open()方法中的代码:

Does anyone know what may be causing this? Here is the code I have in the open() method of my database class:

File sdcard = Environment.getExternalStorageDirectory();

String dbfile = sdcard.getAbsolutePath() + File.separator+ "external_sd" + File.separator + Schema.DATABASE_NAME ;

db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);


推荐答案

这可能是一个可能的错误,设置适当的权限

One possible fault on this could be that you did not set the appropriate permissions

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

然而,将数据存储在SD卡上被认为是不安全的。因为每个拥有在sd存储上存储数据的权限的应用程序都可以访问数据库。

However storing data on a SD Card is considered to be insecure. Because every application which has permissions to store data on the sd storage could access the database.

编辑:另一个可能的问题是外部存储可能不可用。您需要确保您的外部存储设备当前是可写的。一种方法是通过 getExternalStorageState (请参阅参考资料此处)。

Another possible problem is that external storage may not be available. You need to make sure that your external storage is currently writeable. One method would be to determine the state via getExternalStorageState (see reference here).

getExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage. 

参考。在这里检查状态还有一个stackoverflow帖子:编写文本文件到SD卡失败

Reference. There is also a stackoverflow post on checking the state here: Writing Text File to SD Card fails

这篇关于Android打开或创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆