简体   繁体   中英

SQLcipher SQLiteException: error code 14: Could not open database

I have a sqllite cipher database that is stored in Downloads folder of android internal memory file system. I can read it by providing a password with "DB browser for sqllite" in normal way in my desktop from anywhere in HDD. Now I want to import this DB data in my android studio application from standard sqllite openDatabase() command. Kindly suggest me.

I have implemented the following code but it is giving me error-

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


    }


}

Added in build.gradle

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

Error message:

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) 

I got an exception after copying the database file to the Android database directory. After copying the database file, I tried again copying the database to some directory

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

A very amazing thing happened, this piece of code throws an exception

FileNotFound (permission denied)

That because android studio passed not android device user permission

Solution

Copy your database file through assets folder runtime

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

Now you should have the exact permission

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM