1

嗨,我在一个 android 应用程序中使用 sqlcipher 我正在尝试加密一个未加密的数据库,所以目前我收到一条错误消息(无法在事务中附加数据库)所以这是我的代码,请最好帮助我提供一个工作示例

  import java.util.HashMap;

import android.app.SearchManager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;


import android.database.sqlite.SQLiteQueryBuilder;
import android.os.Environment;
import android.provider.BaseColumns;
import android.util.Log;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
//import android.database.sqlite.SQLiteDatabase;
//import android.database.sqlite.SQLiteOpenHelper;

public class DictDatabase {

    private static final String DATABASE_TABLE1 = "words_"+MainModule.DICT;
    private static final String DATABASE_TABLE2 = "dict_"+MainModule.DICT;
    private static final String DATABASE = Environment.getExternalStorageDirectory()+"/beta_"+MainModule.DICT+".sqlite";
    private static final int DATABASE_VERSION = 1;
    public static final int OPEN_READWRITE = 0 ;

public static final String REC_WORDID = "wId";
public static final String REC_WORD2 = "wNormed";
public static final String REC_WORD = "wWord";
public static final String REC_LANG2 = "wLanguage";
    public static final String REC_DESCRIPTION = "wGrammar";
    public static final String REC_SOUND = "wSound";
    public static final String REC_HASH = "wHash";

    private static final String CLASSTAG = "DictDatabase";
     private  SQLiteDatabase mDb;
     private final Context mCtx;
     private DbController dbController;
    //private static final String DATABASE_NAME = "data/data/babla.dict/files/" + "beta_deen.sqlite";

private static class DbController extends SQLiteOpenHelper {
     private SQLiteDatabase mDb;
    private final Context mCtx;
    private static final String words = "CREATE TABLE `words_"+MainModule.DICT+"` (wId INTEGER,wWord ,wNormed ,wLanguage ,wGrammar ,wSound ,wHash )";
    private static final String phrases =
        " CREATE TABLE `phrases_"+MainModule.DICT+"` (Id INTEGER,cat,catPos INTEGER,subCat,subCatPos INTEGER,subSubCat,subSubCatPos INTEGER,entryPos INTEGER,text1,text2,description)";
    private static final String dict =
   " CREATE TABLE `dict"+MainModule.DICT+"` (dId ,dWdeId INTEGER,dWenId INTEGER,dOrigin ,dStatus ,dChangeDate ,transWord1Id ,transWord2Id ,transWord1 ,transWord2 )";
    private static final String enFlex =
    "CREATE TABLE `enFlex` (flexId ,flexPrincipleForm, flexTimeId INTEGER ,flexPersonId, flexGenderId,flexWord,flexWordNormed , flexWordUrl)";
    private static final String flexPersonsGenders =
        "CREATE TABLE `flexPersonsGenders` (fpgLanguage ,fpgPersonId,fpgGenderId ,fpgName)";
    private static final String flexTimes = "CREATE TABLE `flexTimes` (ftId,ftLanguage,ftTimeId,ftName,ftComment)";
    private static final String normalTimesFlex = "CREATE TABLE `normalTimesFlex` (ntfLanguage ,ntfTime  INTEGER )";
    String insertCommand = "insert into "+DATABASE+".words_"+MainModule.DICT+" SELECT * from words_"+MainModule.DICT;
    String insertCommand2 = "insert into "+DATABASE+".dict_"+MainModule.DICT+" SELECT * from dict"+MainModule.DICT;
    String insertCommand3 = "insert into "+DATABASE+".phrases"+MainModule.DICT+" SELECT * from dict"+MainModule.DICT;
    String insertCommand4 = "insert into "+DATABASE+".enFlex SELECT * from enFlex";
    String insertCommand5 = "insert into "+DATABASE+".flexPersonsGenders SELECT * from flexPersonsGenders";
    String insertCommand6 = "insert into "+DATABASE+".flexTimes SELECT * from flexTimes";
    String insertCommand7 = "insert into "+DATABASE+".normalTimesFlex SELECT * from normalTimesFlex";

    DbController(Context context) {
         super(context, DATABASE, null, DATABASE_VERSION);
                     mCtx= context;
     }
    @Override
    public void onCreate(SQLiteDatabase db) {
        mDb = db;
        //mDb = SQLiteDatabase.openOrCreateDatabase(mCtx.getFilesDir() +"/"+"beta_"+MainModule.DICT, "", null);
        //mDb = SQLiteDatabase.openDatabase(mCtx.getFilesDir() +"/"+"beta_"+MainModule.DICT,"",null, SQLiteDatabase.OPEN_READWRITE);
        mDb.execSQL("ATTACH DATABASE '"+DATABASE+"' AS encrypted KEY 'p@ssW0rd';");
        mDb.execSQL(words);mDb.execSQL(phrases);mDb.execSQL(dict);mDb.execSQL(enFlex);mDb.execSQL(flexPersonsGenders);mDb.execSQL(flexTimes);
        mDb.execSQL(normalTimesFlex);
        mDb.execSQL(insertCommand);mDb.execSQL(insertCommand2);mDb.execSQL(insertCommand3);mDb.execSQL(insertCommand4);mDb.execSQL(insertCommand5);
        mDb.execSQL(insertCommand6);mDb.execSQL(insertCommand7);
        mDb.execSQL("SELECT sqlcipher_export('"+DATABASE +"');");
        mDb.execSQL("DETACH DATABASE "+DATABASE+";");
        // TODO Auto-generated method stub
    }

这是它的完成方式还是我把东西放在错误的地方有些人请帮忙,即使有一个例子

4

2 回答 2

0

db.execSQL("ATTACH 'hq_db' AS hq_db;");在开始任何类型的事务之前,您需要以这种方式调用。先做附件,然后开始事务。

于 2013-01-10T08:54:24.570 回答
0

您不需要为每个表运行单独的插入/选择语句,sqlcipher_export便利功能可以为您完成这项工作。有关将未加密的数据库迁移到 SQLCipher 数据库的示例,请查看我们发布的 SQLCipher for Android测试套件中的这个测试用例。

于 2013-01-14T21:09:57.170 回答