net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master
Asked Answered
I

6

11

Error this line :

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

When open the database . but whats Wrong? how to open database with password? Can any one help me?

  1. I set the password on SQLITE Db Browser > File> Set encryption
  2. open this password in android part
  3. When Open then show error
  4. error : net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master

Can any one help me to solve it? thanks in advance

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

    }


}
Iphlgenia answered 7/8, 2018 at 11:48 Comment(0)
I
3

i created a database with sqlcipher V3.5.7 and then changed the sqlcipher version to V4.1.3 and had this problem

in build.gradle i changed

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

to

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

and the problem solved

Involve answered 17/5, 2019 at 13:49 Comment(1)
Instead of using an older version, you can also try to migrate the SQLite database to the new format. Read more here: discuss.zetetic.net/t/upgrading-to-sqlcipher-4/3283Carchemish
C
1

I was getting this exception only in obfuscation case.

I added default constructors for both ByteArraySerializer and ByteArrayDeserializer classes.

Note: don't add above classes as inner classes. Declare these classes independently add keep these classes in proguard-rules.

Cloistered answered 23/4, 2021 at 4:23 Comment(1)
something like ` -keep class ByteArraySerializer { *; } ` ?Filia
C
0

You are referencing the password string value of 123456Sa, however your call within createDataBase uses the value of 123 as a password to getWritableDatabase.

Canella answered 7/8, 2018 at 19:54 Comment(0)
S
0

In my experience, it's always a password issue when you get this error. You can try:

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

And here's how you generate the password and then reset it on your existing database:

  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()
        }
    }
Saldivar answered 22/5, 2023 at 21:17 Comment(0)
S
0

I had this issue simply because I was running a flavor different from the one originally installed in the device.

Staten answered 6/6, 2023 at 15:27 Comment(0)
K
0

So while following the instructions at https://www.zetetic.net/sqlcipher/sqlcipher-for-android/ , I entered the following:

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

… and got the same error you did. Using Android Studio, Device Manager, "Open this device in Device File Explorer" (folder icon), I found out that mkdirs() created //data/data/com.example.whatever/databases/demo.db as a directory!… and so there's your "file is not a database" error. Fix:

databaseFile.getParentFile().mkdirs();
Kerstin answered 21/12, 2023 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.