Android开放或创建数据库数据库、Android

2023-09-06 00:31:43 作者:与可爱私奔

我想创建我的SD卡上的数据库。每当我打电话SQLiteDatabase.openOrCreateDatabase我得到的错误:

  

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

有谁知道什么可能会导致此?这里是code我在我的数据库类的open()方法:

 文件SD卡= Environment.getExternalStorageDirectory();

字符串DBFILE = sdcard.getAbsolutePath()+文件分割符+external_sd+文件分割符+ Schema.DATABASE_NAME;

DB = SQLiteDatabase.openOrCreateDatabase(DBFILE,NULL);
 

解决方案

在这一个可能出现的故障可能是因为你没有设置相应的权限

 <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 

然而存储在SD卡中的数据被认为是不安全的。由于它具有权限的每个应用程序将数据存储在SD存储可以访问数据库。

编辑:另一个可能的问题是,外部存储可能不可用。你需要确保你的外部存储设备,目前可写的。一种方法是通过确定 getExternalStorageState 的状态(见参考文献here).

  getExternalStorageState()返回MEDIA_SHARED如果媒体没有安装,并通过USB大容量存储的共享present。
 
android studio 安卓开发 sqllist数据库,连接数据库,创建数据库,创建数据库表最快捷最简单的方法

Reference.也有在这里检查状态的计算器帖子:的Andr​​oid使用Java编写文本文件到SD卡

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

解决方案

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: Android Java writing text file to sd card