Android 打开或创建数据库

发布于 11-25 15:17 字数 526 浏览 0 评论 0 原文

我正在尝试在我的 SD 卡上创建一个数据库。每当我调用 SQLiteDatabase.openOrCreateDatabase 时,我都会收到错误:

07-21 13:33:17.587: 错误/AndroidRuntime(5541): 原因: android.database.sqlite.SQLiteException:无法打开数据库文件

有谁知道可能导致此问题的原因是什么?这是我的数据库类的 open() 方法中的代码:

File sdcard = Environment.getExternalStorageDirectory();

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

db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

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(5541): Caused by:
android.database.sqlite.SQLiteException: unable to open database file

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

财迷小姐 2024-12-02 15:17:53

一个可能的错误可能是您没有设置适当的权限,

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

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

编辑:另一个可能的问题是外部存储可能不可用。您需要确保您的外部存储当前可写。一种方法是通过 getExternalStorageState 确定状态(请参阅参考文献 "="">此处)。

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

参考。这里还有一个有关检查状态的 stackoverflow 帖子: Writing Text File to SD卡失败

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

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

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.

Edit: 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. 

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

心的憧憬 2024-12-02 15:17:53
public static final String DATABASE_PATH=Environment.getExternalStorageDirectory().getAbsolutePath()+"/WebService/Databases/";


private static final String DATABASE_NAME="database_name.db";


SQLiteDatabase db=SQLiteDatabase.openDatabase(DATABASE_PATH+DATABASE_NAME,null,SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.OPEN_READWRITE);
public static final String DATABASE_PATH=Environment.getExternalStorageDirectory().getAbsolutePath()+"/WebService/Databases/";


private static final String DATABASE_NAME="database_name.db";


SQLiteDatabase db=SQLiteDatabase.openDatabase(DATABASE_PATH+DATABASE_NAME,null,SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.OPEN_READWRITE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文