net.sqlcipher.database.SQLiteException:文件不是数据库:,编译时:select count(*) from sqlite_master

问题描述 投票:0回答:6

此行出错:

mDataBase = SQLiteDatabase.openDatabase(dbPath, "123", null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);

打开数据库时。但有什么问题吗?如何使用密码打开数据库?有人可以帮我吗?

  1. 我在 SQLITE Db Browser > 文件 > 设置加密上设置密码
  2. 在android部分打开此密码
  3. 打开后显示错误
  4. error : net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master

谁能帮我解决这个问题吗?预先感谢

import android.content.Context;
import android.database.SQLException;
//import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
import net.sqlcipher.database.SQLiteDatabase.CursorFactory;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
import net.sqlcipher.database.SQLiteDatabase;
import android.app.Activity;
import android.os.Bundle;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static String TAG = DatabaseHelper.class.getName();
    private static String DB_PATH = "";
    private static String DB_NAME = "ec.db";// Database name
    private SQLiteDatabase mDataBase;
    private final Context mContext;
    File databaseFile=null;
    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
        this.mContext = context;
        SQLiteDatabase.loadLibs(context);
        File databaseFile = context.getDatabasePath(DB_NAME);
        databaseFile.mkdirs();
    }

    public void createDataBase() throws IOException {
        // If database not exists copy it from the assets
        boolean mDataBaseExist = checkDataBase();
        if (!mDataBaseExist) {
            this.getWritableDatabase("123");
            this.close();
            try {
                // Copy the database from assests
                copyDataBase();
                Log.e(TAG, "createDatabase database created");
            } catch (IOException mIOException) {
                throw new Error(mIOException.toString() + " : " + DB_PATH
                        + DB_NAME);// "ErrorCopyingDataBase"
            }
        }
    }

    private boolean checkDataBase() {
        File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
    }

    // Copy the database from assets
    private void copyDataBase() throws IOException {
        InputStream mInput = mContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream mOutput = new FileOutputStream(outFileName);
        byte[] mBuffer = new byte[4096];
        int mLength;
        while ((mLength = mInput.read(mBuffer)) > 0) {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }

    // Open the database, so we can query it
    public boolean openDataBase() throws SQLException {
        String mPath = DB_PATH + DB_NAME;
        //File dbFile = new File(DB_PATH + DB_NAME);
        //File databaseFile = mContext.getDatabasePath(DB_NAME);
        //databaseFile.mkdirs();
        //databaseFile.delete();

        SQLiteDatabase.loadLibs(mContext);

        String dbPath = mContext.getDatabasePath("ec.db").getPath();


        //databaseFile.delete();
        SQLiteDatabase.loadLibs(mContext);
          //mDataBase = SQLiteDatabase.openOrCreateDatabase(databaseFile, "123", null);
        //mDataBase = SQLiteDatabase.openDatabase(mPath, "123",null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        mDataBase = SQLiteDatabase.openDatabase(dbPath, "123", null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);

        return mDataBase != null;
    }

    @Override
    public synchronized void close() {
        if (mDataBase != null)
            mDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


}
android-sqlite sqliteopenhelper sqlcipher sqlcipher-android
6个回答
2
投票

我用sqlcipher V3.5.7创建了一个数据库,然后将sqlcipher版本更改为V4.1.3并遇到了这个问题

在 build.gradle 中我改变了

implementation "net.zetetic:android-database-sqlcipher:4.1.3@aar"

implementation 'net.zetetic:android-database-sqlcipher:3.5.7@aar'

问题就解决了


0
投票

您正在引用

123456Sa
的密码字符串值,但是您在
createDataBase
内的调用使用
123
的值作为
getWritableDatabase
的密码。


0
投票

我仅在混淆情况下才得到此异常。

我为

ByteArraySerializer
ByteArrayDeserializer
类添加了默认构造函数。

注意:不要将上述类添加为内部类。独立声明这些类,在

keep
中添加
proguard-rules
这些类。


0
投票

根据我的经验,当您收到此错误时,始终是密码问题。你可以尝试:

 val encryptedPasskey =
            selectedUserRepository.getSecretKeyEncrypted() ?: saveAndGenerateKey(context, selectedUserRepository)

  val builder = Room.databaseBuilder(
                context.applicationContext,
                CalendarDatabase::class.java,
                DB_NAME
  )

  val factory =                    SupportFactory(SQLiteDatabase.getBytes(encryptedPasskey.toCharArray()))
            val instance = builder
                .openHelperFactory(factory)
  val INSTANCE = builder.build()

以下是生成密码然后在现有数据库上重置密码的方法:

  private fun saveAndGenerateKey(
        context: Context,
        selectedUserRepository: SelectedUserRepository
    ): String {
        val secretKey = KeyGenerator.getInstance(keyType)
            .generateKey().encoded.let { SQLiteDatabase.getChars(it) }.toString()

        selectedUserRepository.setSecretKey(secretKey)

        updateToSecretKey(context = context, secretKey = secretKey)

        return secretKey
    }



private fun updateToSecretKey(context: Context, secretKey: String) {
        val dbFilePath = context.getDatabasePath(DB_NAME)
        if (dbFilePath.exists()) {
            SQLiteDatabase.loadLibs(context)
            val db = SQLiteDatabase.openDatabase(
                dbFilePath.absolutePath,
                "PassPhrase".toByteArray(),
                null,
                SQLiteDatabase.OPEN_READWRITE,
                object : SQLiteDatabaseHook {
                    override fun preKey(p0: SQLiteDatabase?) {
                    }

                    override fun postKey(p0: SQLiteDatabase?) {
                    }
                }
            ) { }

            db.changePassword(secretKey.toCharArray())
            db.close()
        }
    }

0
投票

我遇到这个问题只是因为我运行的版本与设备中最初安装的版本不同。


0
投票

因此,在按照 https://www.zetetic.net/sqlcipher/sqlcipher-for-android/ 的说明进行操作时,我输入了以下内容:

File databaseFile = getDatabasePath("demo.db");
databaseFile.mkdirs();

...并得到了与您相同的错误。使用 Android Studio、设备管理器、“在设备文件资源管理器中打开此设备”(文件夹图标),我发现 mkdirs() 创建了 //data/data/com.example.whatever/databases/demo.db 作为directory!…所以出现了“文件不是数据库”错误。修复:

databaseFile.getParentFile().mkdirs();
© www.soinside.com 2019 - 2024. All rights reserved.