Android加密Sqlite数据库中的错误net.sqlcipher.database.SQLiteException:文件已加密或不是数据库

时间:2014-07-29 08:51:24

标签: android encryption

我创建了一个加密的数据库。

SQLiteDatabase.loadLibs(SplashScreenActivity.this);
        eventsData = new EventDataSQLHelper(this);
        // then you can open the database using a password
        SQLiteDatabase db = eventsData.getWritableDatabase("abc123");


        Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
        Log.v("", "cursor count=" + cursor.getCount());

我在创建db时添加的东西是我有一个加密的db文件,其中已经创建了所有表。我只是将整个容器复制到新创建的数据库。

    public synchronized SQLiteDatabase getWritableDatabase(char[] password) {
            InputStream input = null;
            FileOutputStream output = null;
            int c;
            byte[] tmp;
            if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
                return mDatabase; // The database is already open for business
            }

            if (mIsInitializing) {
                throw new IllegalStateException(
                        "getWritableDatabase called recursively");
            }

            // If we have a read-only database open, someone could be using it
            // (though they shouldn't), which would cause a lock to be held on
            // the file, and our attempts to open the database read-write would
            // fail waiting for the file lock. To prevent that, we acquire the
            // lock on the read-only database, which shuts out other users.

            boolean success = false;
            SQLiteDatabase db = null;
            if (mDatabase != null)
                mDatabase.lock();
            try {
                mIsInitializing = true;
                Log.v(TAG, "mName=" + mName);
                if (mName == null) {
                    db = SQLiteDatabase.create(null, password);

                } else {

                    try {
                        String path = mContext.getDatabasePath(mName).getPath();

                        Log.v(TAG, "path=" + path);
                        File dbPathFile = new File(path);
                        if (!dbPathFile.exists())
                            dbPathFile.getParentFile().mkdirs();

                        output = new FileOutputStream(dbPathFile);

                        //My sqlite file path
                        input = mContext.getResources().openRawResource(
                                R.raw.demo);

                        Log.e(TAG, "Input-" + input);
                        tmp = new byte[1024];
                        int i = 0;
                        while ((c = input.read(tmp)) != -1) {
                            i++;
                            output.write(tmp, 0, c);
                        }

                        db = SQLiteDatabase.openOrCreateDatabase(path, password,
                                mFactory, mHook);
                        Log.e(TAG, "db=" + db);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (input != null) {
                            try {
                                input.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (output != null) {
                            try {
                                output.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }

                int version = db.getVersion();
                if (version != mNewVersion) {
                    db.beginTransaction();
                    try {
                        if (version == 0) {
                            onCreate(db);
                        } else {
                            onUpgrade(db, version, mNewVersion);
                        }
                        db.setVersion(mNewVersion);
                        db.setTransactionSuccessful();
                    } finally {
                        db.endTransaction();
                    }
                }

                onOpen(db);
                success = true;
                return db;
            } finally {
                mIsInitializing = false;
                if (success) {
                    if (mDatabase != null) {
                        try {
                            mDatabase.close();
                        } catch (Exception e) {
                        }
                        mDatabase.unlock();
                    }
                    mDatabase = db;
                } else {
                    if (mDatabase != null)
                        mDatabase.unlock();
                    if (db != null)
                        db.close();
                }
            }
        }

我在此行收到以上错误

simpleQueryForString();

这是int version = db.getVersion();

内的方法

有人知道这个问题吗?帮助我摆脱这个。

0 个答案:

没有答案