SQLcipher SQLiteException:错误代码14:无法打开数据库

时间:2016-10-07 07:07:38

标签: android sqlite sqlcipher-android

我有一个sqllite密码数据库,存储在android内存文件系统的Downloads文件夹中。我可以通过为sqllite"" DB浏览器提供密码来阅读它。在我的桌面上从硬盘的任何地方正常。现在我想从标准sqllite openDatabase()命令导入我的android studio应用程序中的这个DB数据。请建议我。

我已经实现了以下代码,但它给了我错误 -

package com.example.k1.sqlliteload;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.database.Cursor;
import android.os.Environment;
import android.widget.TextView;

import net.sqlcipher.database.SQLiteDatabase;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);`enter code here`
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        SQLiteDatabase.loadLibs(this);
        openDB();

    }

    void openDB() {

        String fileLoc = Environment.getExternalStorageDirectory() + "/Download/" + "b.db";
        SQLiteDatabase mydatabase = SQLiteDatabase.openDatabase(fileLoc, "123", null, SQLiteDatabase.OPEN_READWRITE);


        //--------------Select all rows--------------------------------------------
        Cursor cursor1 = mydatabase.rawQuery("select * from conmast", null);
        if (cursor1.moveToFirst()) {
            while (cursor1.isAfterLast() == false) {
                String c11 = String.valueOf(cursor1.getInt(0));
                String c22 = cursor1.getString(1);
                tv.append(c11 + " " + c22 + " " + "\n");
                cursor1.moveToNext();
            }

        }

        cursor1.close();

        tv.append("//////////////////////////////////////////////////\n");


    }


}

在build.gradle中添加

 compile 'net.zetetic:android-database-sqlcipher:3.5.4@aar'

错误讯息:

10-07 12:20:36.224 12237-12237/com.example.k1.sqlliteload E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.k1.sqlliteload, PID: 12237
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.k1.sqlliteload/com.example.k1.sqlliteload.MainActivity}: 
net.sqlcipher.database.SQLiteException: error code 14: Could not open database

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Caused by: net.sqlcipher.database.SQLiteException: error code 14: Could not open database
at net.sqlcipher.database.SQLiteDatabase.dbopen(Native Method)
at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(SQLiteDatabase.java:2353)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1116)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1083)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1032)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
at com.example.k1.sqlliteload.MainActivity.openDB(MainActivity.java:32)
at com.example.k1.sqlliteload.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:6323)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method) 

1 个答案:

答案 0 :(得分:0)

将数据库文件复制到Android database目录后,出现异常。复制database文件后,我再次尝试将数据库复制到某个目录

val src = getDatabasePath(srcName)
        if (src.exists()){
            src.parent?.run {
                src.copyTo(File(File(this), destName))
            }
        }

发生了一件非常了不起的事情,这段代码引发了异常

FileNotFound(权限被拒绝)

那是因为android studio没有通过 android设备用户权限

解决方案

通过assets文件夹运行时复制数据库文件

copyFile(assets.open("src.db"), FileOutputStream(getDatabasePath("dest.db")))

现在您应该拥有确切的permission