嗨我在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
}
这是它的完成方式,或者我把东西放在错误的地方,即使有一个例子也请帮助
答案 0 :(得分:0)
在开始任何类型的交易之前,您需要以这种方式调用db.execSQL("ATTACH 'hq_db' AS hq_db;");
。先做附件,然后开始交易。
答案 1 :(得分:0)
您不需要为每个表运行单独的insert / select语句,sqlcipher_export
便利功能可以帮助您。有关将未加密的数据库迁移到SQLCipher数据库的示例,请查看我们发布的this测试用例。