4

我有一个 sqllite 密码数据库,它存储在 android 内存文件系统的下载文件夹中。我可以通过在硬盘中的任何位置以正常方式在我的桌面上使用“DB browser for sqllite”提供密码来阅读它。现在我想从标准的 sqllite openDatabase() 命令在我的 android studio 应用程序中导入这个数据库数据。请建议我。

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

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) 
4

1 回答 1

0

database将数据库文件复制到 Android目录后出现异常。复制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

于 2020-02-21T20:31:54.877 回答