日期:2014-05-16 浏览次数:20686 次
MainActivity如下:
package cc.testsqlcipher; import net.sqlcipher.Cursor; import net.sqlcipher.database.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; /** * Demo描述: * 使用SQLCipher对SQLite数据库进行加密 * * 参考资料: * 1 http://blog.csdn.net/guolin_blog/article/details/11952409 * 2 http://blog.csdn.net/zhuawami/article/details/9038003 * Thank you very much */ public class MainActivity extends Activity { private Button mAddButton; private Button mQueryButton; private SQLiteDatabase mSqLiteDatabase; private SQLCipherOpenHelper mSqlCipherOpenHelper; private final String SECRET_KEY="95279527"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); initDataBase(); } private void initView(){ mAddButton=(Button) findViewById(R.id.addButton); mAddButton.setOnClickListener(new ClickListenerImpl()); mQueryButton=(Button) findViewById(R.id.queryButton); mQueryButton.setOnClickListener(new ClickListenerImpl()); } private void initDataBase() { //加载libs/armeabi中的so SQLiteDatabase.loadLibs(this); //获取到SQLiteOpenHelper mSqlCipherOpenHelper=new SQLCipherOpenHelper(this); //设置打开数据库的密码SECRET_KEY mSqLiteDatabase = mSqlCipherOpenHelper.getWritableDatabase(SECRET_KEY); } private class ClickListenerImpl implements OnClickListener { private Person person; @Override public void onClick(View v) { switch (v.getId()) { case R.id.addButton: for (int i = 0; i < 15; i++) { person = new Person("xiaoming" + i, "9527" + i); addData(person); } break; case R.id.queryButton: person=queryData(8); System.out.println(""+person.toString()); break; default: break; } } } public void addData(Person person) { mSqLiteDatabase.execSQL("insert into person (name,phone) values(?,?)", new Object[] {person.getName(), person.getPhone() }); } public Person queryData(int id){ Cursor cursor=mSqLiteDatabase.rawQuery("select * from person where personid=?", new String[]{String.valueOf(id)}); while(cursor.moveToFirst()){ int personid=cursor.getInt(cursor.getColumnIndex("personid")); String name=cursor.getString(cursor.getColumnIndex("name")); String phone=cursor.getString(cursor.getColumnIndex("phone")); return new Person(personid, name, phone); } cursor.close(); return null; } }
SQLCipherOpenHelper如下:
package cc.testsqlcipher; import android.content.Context; import net.sqlcipher.database.SQLiteDatabase; import net.sqlcipher.database.SQLiteOpenHelper; /** * 注意: * 这里包的引用,均是在: * net.sqlcipher.database之下 */ public class SQLCipherOpenHelper extends SQLiteOpenHelper { private final static String DATABASE_NAME="test.db"; public SQLCipherOpenHelper(Context context) { super(context, DATABASE_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table person(personid integer primary key autoincrement,name varchar(20),phone VARCHAR(12))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
Person如下:
package cc.testsqlcipher; public class Person { private Integer id; private String name; private String phone; public Person(String name, String phone) { this.name = name; this.phone = phone; } public Person(Integer id, String name, String phone) { this.id = id; this.name = name; this.phone = phone; } public In