无法打开数据库的Andr​​oid的Java的SQLite [英] Failed to open Database Android Java SQLite

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

问题描述

下面是我的code:

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    SQLiteDatabase.openOrCreateDatabase(/ gregpeck.db,NULL);}

显然,这是我的主要活动中。

我还添加了允许我的主要活动:

 <清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        包=ie.callanan.dennis.testhw>        <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>        <应用
            机器人:allowBackup =真

我收到错误消息是:

 无法打开数据库'gregpeck.db。
android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(code 14):无法打开数据库


解决方案

我会建议使用 SQLiteOpenHelper ,如果您需要为您的应用程序专用数据库:

 公共类MyDatabaseHelper扩展SQLiteOpenHelper {    私有静态最后弦乐DATABASE_NAME =数据库名;
    私有静态最终诠释DATABASE_VERSION = 1;
    私有静态最后弦乐TABLE_CREATE =CREATE TABLE ......;    公共MyDatabaseHelper(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
    }    @覆盖
    公共无效的onCreate(SQLiteDatabase数据库){
        database.execSQL(TABLE_CREATE);
    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase数据库,诠释oldVersion,诠释静态网页){
        //做任何升级需要
    }
}

这里你找到一个完整的例子。如果你想打开从SD卡数据库,使用:

 文件文件=新的文件(/ SD卡/ db.sqlite);
SQLiteDatabase DB = SQLiteDatabase.openOrCreateDatabase(文件,NULL);
Log.d(MyApp的,成功打开:+ db.isOpen());

对于第一种情况下,你不需要任何特殊的权限。对于第二个你这样做。的注意的:Android的4.3和4.4做限制SD卡上的访问。所以它可能是,你不能在所有访问数据库文件(例如见该<一个href=\"http://www.theguardian.com/technology/askjack/2014/oct/09/how-can-i-get-android-kitkat-to-work-with-my-sd-cards\"相对=nofollow>文章)。

Here is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SQLiteDatabase.openOrCreateDatabase("/gregpeck.db", null);

}

Obviously this is inside my Main Activity.

I have also added the permission to my Main Activity:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="ie.callanan.dennis.testhw" >

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

        <application
            android:allowBackup="true"

The error message I receive is:

Failed to open database 'gregpeck.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

解决方案

I would recommend to use a SQLiteOpenHelper, if you need a private database for your application:

public class MyDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "DatabaseName";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_CREATE = "CREATE TABLE ....";

    public MyDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database,int oldVersion,int newVersion){
        // do whatever is required for the upgrade 
    }
}

Here you find a full example. If you want to open a database from SD card, use:

File file = new File("/sdcard/db.sqlite" ); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(file, null);
Log.d("MyApp", "Successfully opened: "  + db.isOpen());

For the first case you don't need any particular permissions. For the second one you do. Note: Android 4.3 and 4.4 do restrict the access on the SD card. So it might be that you cannot access the database file at all (see for instance this article).

这篇关于无法打开数据库的Andr​​oid的Java的SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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