1、下载官方支持包:https://s3.amazonaws.com/sqlcipher/3.2.0/sqlcipher-for-android-community-v3.2.0.zip

Github地址:https://github.com/sqlcipher/android-database-sqlcipher

2、本博客使用Android Studio开发,Eclipse怎样使用Sqlcipher大家可以到网上搜索。

3、需要导入以下包及文件。如何导入Jar包可以参考我的博客http://www.cnblogs.com/begin1949/p/4966542.html。

4、我们重写一下SqliteOpenHelper类。这里注意一下引用的类来自于net.sqlcipher.database而不是谷歌官方的sqlite包。

import android.content.Context;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper; public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_TABLE = "create table Book(name text,pages integer)"; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory
factory, int version) {
super(context, name, factory, version);
} @Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE);
} @Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}

5、接下来即可使用了。

import android.content.ContentValues;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import com.sqlcipher.R; import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase; public class Use1Activity extends FragmentActivity implements View.OnClickListener { private SQLiteDatabase db;
private MyDatabaseHelper dbHelper; private Button mBtnAdd;
private Button mBtnQuery;
private TextView mTvShow; private String result; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_use1); initView();
} private void initView() {
SQLiteDatabase.loadLibs(this);
dbHelper = new MyDatabaseHelper(this, "demo.db", null, 1);
db = dbHelper.getWritableDatabase("secret_key"); mBtnAdd = (Button) findViewById(R.id.add_data);
mBtnQuery = (Button) findViewById(R.id.query_data);
mTvShow = (TextView) findViewById(R.id.tv_show); mBtnAdd.setOnClickListener(this);
mBtnQuery.setOnClickListener(this);
} @Override
public void onClick(View v) {
if (v == mBtnAdd) {
ContentValues values = new ContentValues();
values.put("name", "密码");
values.put("pages", 566);
db.insert("Book", null, values);
} else if (v == mBtnQuery) {
Cursor cursor = db.query("Book", null, null, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
result += "book name is " + name + "\n";
result += "book pages is " + pages + "\n";
}
}
cursor.close(); mTvShow.setText(result);
result = "";
}
}
}

6、参考博文:https://discuss.zetetic.net/t/android-studio-integration/65

http://blog.csdn.net/sziicool/article/details/18728153

最新文章

  1. jquery中的ajax方法参数总是记不住,这里记录一下。
  2. visio二次开发初始化问题
  3. 第三章 深入 ZAB 协议
  4. css 关于两栏布局,左边固定,右边自适应
  5. 最简单的CRC32源码---逐BIT法
  6. VS2012(update3)编译Qt5.1.1 32位静态库debug-and-release版及结果分享
  7. table的样式定义
  8. Leetcode dfs Combination SumII
  9. Django: 之用户注册、缓存和静态网页
  10. ZipFile和ZipInputSteam解压zip文件
  11. 自定义checkbox, radio样式
  12. 支持ipV6和ipV4的客户端编程
  13. MSSQL 2000 错误823恢复
  14. 利用HTP工具包开发报表
  15. Java知多少(74)基础类库
  16. SAP按销售订单生产和标准结算配置及操作手册
  17. 多线程处理慢sql查询小笔记~
  18. Codeforces Round #354 (Div. 2) D. Theseus and labyrinth bfs
  19. .NET Core调用WCF的最佳实践
  20. Spring @Autowired、@Resource、@Required、@Component、@Repository、@Service、@Controller注解的用法和作用

热门文章

  1. HTML5小游戏UI美化版
  2. 利用WCF与Android实现图片上传并传参
  3. (Java 多线程系列)Java 线程池(Executor)
  4. Go Revel - Parameters(参数绑定)
  5. jQuery焦点图切换特效插件封装
  6. 原生javascript-图片按钮切换
  7. Dom4j下载及使用Dom4j读写XML简介
  8. All about Performing User-Managed Database Recovery
  9. ASP.NET控件Repeter的使用
  10. https时代来了,你却还一无所知?