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

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

问题描述

我想创建我的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 的状态(见参考文献<一href="http://developer.android.com/reference/android/os/Environment.html#getExternalStorageState%28%29">here).

  getExternalStorageState()返回MEDIA_SHARED如果媒体没有安装,并通过USB大容量存储的共享present。
 

<一个href="http://developer.android.com/reference/android/os/Environment.html#MEDIA_SHARED">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

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

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