我正在努力解决如何使用 SQLCipher 加密/解密数据库。
在我的这段代码中,我应该从哪里开始。以及如何开始。
如果可能,请编辑此代码。
我关注了这个链接,但没有得到如何做到这一点。http://sqlcipher.net/sqlcipher-api/#key
我的数据库文件:-
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
import android.content.Context;
import android.util.Log;
public class DatabaseClass extends SQLiteOpenHelper
{
/**
* Constants
*/
public final static String DATABASE_NAME ="mycipherdatabase.db";
public final static String NAME ="name";
public final static String ADDRESS ="address";
public final static String CITY ="city";
public static String pass = "1234";
public DatabaseClass(Context context) {
super(context, DATABASE_NAME, null, 1);
}
/**
* called once
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( "CREATE TABLE mylistdata(_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, address TEXT,city TEXT);");
Log.v("inside", "oncreate");
}
/**
* called when we upgrade(change)the version number of database
* onCreate also called after changing the version
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP Table mylistdata");
onCreate(db);
}
}
使用此数据库的其他活动:-
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SQLiteDatabase.loadLibs(this);
File file = getDatabasePath(DatabaseClass.DATABASE_NAME);
file.mkdir();
file.delete();
sqldb = SQLiteDatabase.openOrCreateDatabase(file,key, null);
db = new DatabaseClass(this);
etname = (EditText)findViewById(R.id.name);
etadd = (EditText)findViewById(R.id.add);
etcity = (EditText)findViewById(R.id.city);
result = (ListView)findViewById(R.id.mylist);
}
/**
*insert data in to database
*/
public void saveData(View v) {
ContentValues insertData = new ContentValues();
insertData.put(DatabaseClass.NAME, etname.getText().toString());
insertData.put(DatabaseClass.ADDRESS, etadd.getText().toString());
insertData.put(DatabaseClass.CITY, etcity.getText().toString());
db.getWritableDatabase(key).insert("mylistdata", DatabaseClass.NAME , insertData);
db.close();
resetEditText();
Toast.makeText(this,"Data saved", Toast.LENGTH_SHORT).show();
}