SQLiteCantOpenDatabaseException

SQLiteCantOpenDatabaseException

本文介绍了android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(code 14):无法打开数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读各种网站上关于这个问题,但我不能算出这个。我使用pre-构建数据库为这个应用程序。我使用的软糖为这个应用程序。

AndroidManifest.xml中

 < XML版本=1.0编码=UTF-8&GT?;
    <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        包=com.example.quotes
        安卓版code =1
        机器人:VERSIONNAME =1.0>

        <用途-SDK
            安卓的minSdkVersion =8
            机器人:targetSdkVersion =17/>

        <应用
            机器人:allowBackup =真
            机器人:图标=@可绘制/ ic_launcher
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@风格/ AppTheme>
            <活动
                机器人:名称=com.example.quotes.MainActivity
                机器人:标签=@字符串/ APP_NAME>
                <意向滤光器>
                    <作用机器人:名称=android.intent.action.MAIN/>

                    <类机器人:名称=android.intent.category.LAUNCHER/>
                &所述; /意图滤光器>
            < /活性GT;
        < /用途>

    < /舱单>
 

logcat的输出

根据logcat的它造成SQLiteCantOpenDatabaseException

  06-10 23:07:01.831:E /跟踪(4419):错误打开跟踪文件:没有这样的文件或目录(2)
06-10 23:07:03.611:E / SQLiteLog(4419):(14)无法打开文件在30176行[00bb9c9ce4]
06-10 23:07:03.621:E / SQLiteLog(4419):(14)os_unix.c:30176:(2)开(/data/data/com.example.quotes/databasesQuotesdb) -
06-10 23:07:03.641:E / SQLiteDatabase(4419):无法打开数据库'/data/data/com.example.quotes/databasesQuotesdb。
06-10 23:07:03.641:E / SQLiteDatabase(4419):android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(code 14):无法打开数据库
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnection.nativeOpen(本机方法)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804​​)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在com.example.quotes.DataBaseHelper.checkDataBase(DataBaseHelper.java:94)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在com.example.quotes.DataBaseHelper.createDataBase(DataBaseHelper.java:58)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在com.example.quotes.MainActivity.onCreate(MainActivity.java:34)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.app.Activity.performCreate(Activity.java:5104)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.os.Handler.dispatchMessage(Handler.java:99)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.os.Looper.loop(Looper.java:137)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在android.app.ActivityThread.main(ActivityThread.java:5041)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在java.lang.reflect.Method.invokeNative(本机方法)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在java.lang.reflect.Method.invoke(Method.java:511)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-10 23:07:03.641:E / SQLiteDatabase(4419):在dalvik.system.NativeStart.main(本机方法)
06-10 23:07:03.731:E / SQLiteLog(4419):(14)无法打开文件在30176行[00bb9c9ce4]
06-10 23:07:03.731:E / SQLiteLog(4419):(14)os_unix.c:30176:(2)开(/data/data/com.example.quotes/databasesQuotesdb) -
06-10 23:07:03.781:E / SQLiteDatabase(4419):无法打开数据库'/data/data/com.example.quotes/databasesQuotesdb。
06-10 23:07:03.781:E / SQLiteDatabase(4419):android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(code 14):无法打开数据库
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnection.nativeOpen(本机方法)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804​​)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在com.example.quotes.DataBaseHelper.openDataBase(DataBaseHelper.java:145)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在com.example.quotes.MainActivity.onCreate(MainActivity.java:44)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.app.Activity.performCreate(Activity.java:5104)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.os.Handler.dispatchMessage(Handler.java:99)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.os.Looper.loop(Looper.java:137)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在android.app.ActivityThread.main(ActivityThread.java:5041)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在java.lang.reflect.Method.invokeNative(本机方法)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在java.lang.reflect.Method.invoke(Method.java:511)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-10 23:07:03.781:E / SQLiteDatabase(4419):在dalvik.system.NativeStart.main(本机方法)
06-10 23:07:03.791:D / AndroidRuntime(4419):关闭虚拟机
06-10 23:07:03.791:W / dalvikvm(4419):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40a71930)
06-10 23:07:03.831:E / AndroidRuntime(4419):致命异常:主要
06-10 23:07:03.831:E / AndroidRuntime(4419):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.quotes / com.example.quotes.MainActivity}:android.database.sqlite.SQLiteCantOpenDatabaseException :未知错误(code 14):无法打开数据库
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.os.Handler.dispatchMessage(Handler.java:99)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.os.Looper.loop(Looper.java:137)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.app.ActivityThread.main(ActivityThread.java:5041)
06-10 23:07:03.831:E / AndroidRuntime(4419):在java.lang.reflect.Method.invokeNative(本机方法)
06-10 23:07:03.831:E / AndroidRuntime(4419):在java.lang.reflect.Method.invoke(Method.java:511)
06-10 23:07:03.831:E / AndroidRuntime(4419):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
06-10 23:07:03.831:E / AndroidRuntime(4419):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-10 23:07:03.831:E / AndroidRuntime(4419):在dalvik.system.NativeStart.main(本机方法)
06-10 23:07:03.831:E / AndroidRuntime(4419):由:android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(code 14):无法打开数据库
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.database.sqlite.SQLiteConnection.nativeOpen(本机方法)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804​​)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
06-10 23:07:03.831:E / AndroidRuntime(4419):在com.example.quotes.DataBaseHelper.openDataBase(DataBaseHelper.java:145)
06-10 23:07:03.831:E / AndroidRuntime(4419):在com.example.quotes.MainActivity.onCreate(MainActivity.java:44)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.app.Activity.performCreate(Activity.java:5104)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-10 23:07:03.831:E / AndroidRuntime(4419):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-10 23:07:03.831:E / AndroidRuntime(4419):11 ...更多
06-10 23:07:04.083:D / dalvikvm(4419):GC_CONCURRENT释放203K,11%免费2676K / 3000K,暂停25ms的+ 18毫秒,总306ms
06-10 23:07:07.811:I /过程(4419):发送信号。 PID:4419 SIG:9
 

DataBaseHelper.java

 公共类DataBaseHelper扩展SQLiteOpenHelper {

    //而Android的默认系统应用程序的数据库路径。
    私有静态字符串DB_PATH =/data/data/com.example.quotes/databases;

    私有静态字符串DB_NAME =Quotesdb;

    //数据库的表名。
    静态最后弦乐TABLE_NAME =行情;

    //联系方式表列名
    //私有静态最后弦乐_id =_id;
    //私有静态最后弦乐报价=引号;
    //私有静态最后弦乐作者=作者;

    私人SQLiteDatabase MyDatabase的;

    私人最终语境myContext;

    / **
     *构造函数
     *注意到并保持传递的上下文中的一个参考,以便访问该应用程序的资产和资源。
     * @参数方面
     * /
    公共DataBaseHelper(上下文的背景下){
        超级(上下文,DB_NAME,空,1);
        this.myContext =背景;
    }

    / **
     *在系统上创建一个空数据库,并与自己的数据库重写它。
     * * /
    公共无效的CreateDatabase()抛出IOException异常{

        布尔dbExist = checkDataBase();
        SQLiteDatabase db_Read = NULL;

        如果(dbExist){
            //什么也不做 - 数据库中已存在
        }其他{
            //通过调用这个方法和空的数据库将被创建到系统​​默认路径
            //你的应用程序,所以我们要能够覆盖数据库,我们的数据库。
            db_Read = this.getReadableDatabase();
            db_Read.close();

            尝试 {
                copyDataBase();
            }赶上(IOException异常E){
                //抛出新的错误(错误复制数据库);
                e.toString();
            }
        }
    }

    / **
     *检查数据库中已存在,以避免重新复制每次打开应用程序时,该文件。
     * @返回true,如果它存在,虚假的,如果它不
     * /
    私人布尔checkDataBase(){

        SQLiteDatabase CHECKDB = NULL;

        尝试{
            字符串mypath中= DB_PATH + DB_NAME;
            CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
        }赶上(SQLiteException E){
            //数据库的简化版,存在。
        }

        如果(CHECKDB!= NULL){
            checkDB.close();
        }

        返回CHECKDB!= NULL?真假;
    }

    / **
     *复制你的数据库从当地的资产文件夹在刚才创建的空数据库
     *系统文件夹,从那里它可以被访问和处理。
     *这是通过transfering字节流进行。
     * * /
    私人无效copyDataBase()抛出IOException异常{

        //打开本地数据库作为输入流
        InputStream的myInput = myContext.getAssets()开(DB_NAME)。

        //路径刚刚创建的空分贝
        字符串outFileName = DB_PATH + DB_NAME;

        //打开空分贝的输出流
        的OutputStream myOutput =新的FileOutputStream(outFileName);

        //传输的字节从inputfile中的OUTPUTFILE
        byte []的缓冲区=新的字节[1024];
        INT长;
        而((长度= myInput.read(缓冲液))大于0){
            myOutput.write(缓冲液,0,长度);
        }

        //关闭流
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    公共无效的openDatabase()抛出的SQLException {
        //打开数据库
        字符串mypath中= DB_PATH + DB_NAME;
        MyDatabase的= SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);

    }

    @覆盖
    市民同步无效的close(){
        如果(MyDatabase的!= NULL)
            myDataBase.close();

        super.close();
    }

    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        // TODO自动生成方法存根
    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        // TODO自动生成方法存根
    }

    / * //获取单个联系人
    公开名单<行情>为getQuote(INT ID){
        ArrayList的<行情>();
        getReadableDatabase();

        光标光标= db.query(TABLE_NAME,新的String [] {_id,
                qotes,笔者},_id +=?,新的String [] {将String.valueOf(ID)},
                NULL,NULL,NULL,NULL);

        //查询字符串=SELECT _id,qotes,笔者从+ TABLE_NAME;
        //查询字符串=SELECT * FROM行情;

        //光标光标= db.rawQuery(查询,NULL);

        如果(光标!= NULL)
            cursor.moveToFirst();
        行情报价=新行情(的Integer.parseInt(cursor.getString(0)),
        cursor.getString(1),cursor.getString(2));
        QuoteList.add(引号);
        返回QuoteList;
    }
    * /
}
 

解决方案

添加此权限到项目

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

I have read various on site about this issue but I am not able to figure this out. I am using pre-build database for this app. I am using jellybean for this app.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.quotes"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.quotes.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

Logcat Output

according to logcat it's caused by SQLiteCantOpenDatabaseException

06-10 23:07:01.831: E/Trace(4419): error opening trace file: No such file or directory (2)
06-10 23:07:03.611: E/SQLiteLog(4419): (14) cannot open file at line 30176 of [00bb9c9ce4]
06-10 23:07:03.621: E/SQLiteLog(4419): (14) os_unix.c:30176: (2) open(/data/data/com.example.quotes/databasesQuotesdb) -
06-10 23:07:03.641: E/SQLiteDatabase(4419): Failed to open database '/data/data/com.example.quotes/databasesQuotesdb'.
06-10 23:07:03.641: E/SQLiteDatabase(4419): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at com.example.quotes.DataBaseHelper.checkDataBase(DataBaseHelper.java:94)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at com.example.quotes.DataBaseHelper.createDataBase(DataBaseHelper.java:58)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at com.example.quotes.MainActivity.onCreate(MainActivity.java:34)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.app.Activity.performCreate(Activity.java:5104)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.os.Looper.loop(Looper.java:137)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at java.lang.reflect.Method.invokeNative(Native Method)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at java.lang.reflect.Method.invoke(Method.java:511)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-10 23:07:03.641: E/SQLiteDatabase(4419):     at dalvik.system.NativeStart.main(Native Method)
06-10 23:07:03.731: E/SQLiteLog(4419): (14) cannot open file at line 30176 of [00bb9c9ce4]
06-10 23:07:03.731: E/SQLiteLog(4419): (14) os_unix.c:30176: (2) open(/data/data/com.example.quotes/databasesQuotesdb) -
06-10 23:07:03.781: E/SQLiteDatabase(4419): Failed to open database '/data/data/com.example.quotes/databasesQuotesdb'.
06-10 23:07:03.781: E/SQLiteDatabase(4419): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at com.example.quotes.DataBaseHelper.openDataBase(DataBaseHelper.java:145)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at com.example.quotes.MainActivity.onCreate(MainActivity.java:44)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.app.Activity.performCreate(Activity.java:5104)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.os.Looper.loop(Looper.java:137)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at java.lang.reflect.Method.invokeNative(Native Method)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at java.lang.reflect.Method.invoke(Method.java:511)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-10 23:07:03.781: E/SQLiteDatabase(4419):     at dalvik.system.NativeStart.main(Native Method)
06-10 23:07:03.791: D/AndroidRuntime(4419): Shutting down VM
06-10 23:07:03.791: W/dalvikvm(4419): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-10 23:07:03.831: E/AndroidRuntime(4419): FATAL EXCEPTION: main
06-10 23:07:03.831: E/AndroidRuntime(4419): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.quotes/com.example.quotes.MainActivity}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.os.Looper.loop(Looper.java:137)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at java.lang.reflect.Method.invokeNative(Native Method)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at java.lang.reflect.Method.invoke(Method.java:511)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at dalvik.system.NativeStart.main(Native Method)
06-10 23:07:03.831: E/AndroidRuntime(4419): Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at com.example.quotes.DataBaseHelper.openDataBase(DataBaseHelper.java:145)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at com.example.quotes.MainActivity.onCreate(MainActivity.java:44)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.app.Activity.performCreate(Activity.java:5104)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-10 23:07:03.831: E/AndroidRuntime(4419):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-10 23:07:03.831: E/AndroidRuntime(4419):     ... 11 more
06-10 23:07:04.083: D/dalvikvm(4419): GC_CONCURRENT freed 203K, 11% free 2676K/3000K, paused 25ms+18ms, total 306ms
06-10 23:07:07.811: I/Process(4419): Sending signal. PID: 4419 SIG: 9

DataBaseHelper.java

public class DataBaseHelper extends SQLiteOpenHelper{

    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/com.example.quotes/databases";

    private static String DB_NAME = "Quotesdb";

    // Table Names of Data Base.
    static final String TABLE_Name = "Quotes";

    // Contacts Table Columns names
    //private static final String _Id = "_Id";
    //private static final String quotes = "quotes";
    //private static final String author = "author";

    private SQLiteDatabase myDataBase;

    private final Context myContext;

    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    /**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();
        SQLiteDatabase db_Read = null;

        if(dbExist){
            //do nothing - database already exist
        }else{
            //By calling this method and empty database will be created into the default system path
            //of your application so we are gonna be able to overwrite that database with our database.
            db_Read = this.getReadableDatabase();
            db_Read.close();

            try {
                copyDataBase();
            } catch (IOException e) {
                //throw new Error("Error copying database");
                e.toString();
            }
        }
    }

    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase(){

        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        }catch(SQLiteException e){
            //database does't exist yet.
        }

        if(checkDB != null){
            checkDB.close();
        }

        return checkDB != null ? true : false;
    }

    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    public void openDataBase() throws SQLException{
        //Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {
        if(myDataBase != null)
            myDataBase.close();

        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
    }

    /*// Getting single contact
    public List<Quotes> getQuote(int id) {
        ArrayList<Quotes>();
        getReadableDatabase();

        Cursor cursor = db.query(TABLE_Name, new String[] { _Id,
                qotes,author }, _Id + "=?", new String[] { String.valueOf(id) },
                null, null, null, null);

        //String query = "SELECT _Id, qotes, author From "+TABLE_Name;
        //String query = "SELECT * From Quotes";

        //Cursor cursor = db.rawQuery(query, null);

        if (cursor != null)
            cursor.moveToFirst();
        Quotes quotes = new Quotes(Integer.parseInt(cursor.getString(0)),
        cursor.getString(1),cursor.getString(2));
        QuoteList.add(quotes);
        return QuoteList;
    }
    */
}
解决方案

Add this permission to your project

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

这篇关于android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(code 14):无法打开数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 12:24