android数据库除了本身自带的数据库外,在一些其他行业的安全领域经常会用到加密数据库的使用。
使用步骤:
1.在build.gradle添加:
apply plugin: 'com.android.application'
//....
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'net.zetetic:android-database-sqlcipher:3.5.3'
}
2.初始化依赖库:
package dome.netsqlite.com.netsqlitedome;
import android.app.Application;
import net.sqlcipher.database.SQLiteDatabase;
public class MyApplication extends Application {
public static MyApplication getInstance() {
return mApp;
}
private static MyApplication mApp;
@Override
public void onCreate() {
super.onCreate();
//初始化依赖库
SQLiteDatabase.loadLibs(this);
mApp = this;
}
}
3.NetSQLiteHelper
package dome.netsqlite.com.netsqlitedome;
import android.content.Context;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
/**
* android 网络安全数据测试
* Created by chenkui on 2016/11/12.
*/
public class NetSQLiteHelper extends SQLiteOpenHelper {
public static String SQLITE_NAME="NET_SQLITE";
public static int VERSION=1;
private NetSQLiteHelper(Context context) {
super(context, SQLITE_NAME,null, VERSION);
}
public static SQLiteDatabase onOpen() {
SQLiteDatabase db=new NetSQLiteHelper(MyApplication.getInstance()).getWritableDatabase("123");
return db;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// String sql="create table if not exists person " +
// "(_id INTEGER PRIMARY KEY autoincrement,"+
// "person_name text,"+
// "person_age text,"+
// "person_sex text)";
String sql="create table if not exists person " +
"(_id text PRIMARY KEY,"+
"person_name text,"+
"person_age text,"+
"person_sex text)";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
4.NetSQLiteUitl,这里我写了几种调用类似系统,还直接使用SQ语句进行增删改查的几种类似使用方法
package dome.netsqlite.com.netsqlitedome;
import android.content.ContentValues;
import android.database.Cursor;
import net.sqlcipher.database.SQLiteDatabase;
/**
* Created by chenkui on 2016/11/13.
*/
public class NetSQLiteUitl {
public void insert(String id,String name, String age, String sex) {
SQLiteDatabase db = NetSQLiteHelper.onOpen();
ContentValues values = new ContentValues();
values.put("_id", id);
values.put("person_name", name);
values.put("person_age", age);
values.put("person_sex", sex);
db.insert("person", null, values);
db.close();
}
public void insertBySql(String _id, String name, String age) {
SQLiteDatabase db = NetSQLiteHelper.onOpen();
db.execSQL("insert into person(_id,person_name,person_age) values(?,?,?)", new String[]{_id, name, age});
db.close();
}
public String queryAll() {
SQLiteDatabase db = NetSQLiteHelper.onOpen();
Cursor cursor = db.query("person", null, null, null, null, null, null);
StringBuffer sb = new StringBuffer();
// int index = -1;
sb.append("person:");
while (cursor.moveToNext()) {
String[] names = cursor.getColumnNames();
for (int i = 0; i < names.length; i++) {
// cursor.getColumnName(cursor.)
// sb.append(names[i]);
// index=cursor.getInt(cursor.getColumnIndex(names[i]));
// index=cursor.getInt(cursor.getColumnIndex("_id"));
// sb.append(index).append("===");
}
// index = cursor.getInt(cursor.getColumnIndex("_id"));
String index = cursor.getString(cursor.getColumnIndex("_id"));
sb.append("{").append("_id").append(":").append(index).append(",");
String person_name = cursor.getString(cursor.getColumnIndex("person_name"));
sb.append("person_name").append(":").append(person_name).append(",");
String person_age= cursor.getString(cursor.getColumnIndex("person_age"));
sb.append("person_age").append(":").append(person_age).append(",");
String person_sex= cursor.getString(cursor.getColumnIndex("person_sex"));
sb.append("person_sex").append(":").append(person_sex).append("}");
}
cursor.close();
db.close();
return sb.toString();
}
public String queryBySql() {
SQLiteDatabase db = NetSQLiteHelper.onOpen();
Cursor cursor = db.rawQuery("select * from person", null);
String personStr="";
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("person_name"));
String age=cursor.getString(cursor.getColumnIndex("person_age"));
personStr+="{id:"+id+",name:"+name+",age:"+age+"}"+"\n";
}
cursor.close();
db.close();
return personStr;
}
public void upDateBySql(String name, String id) {
SQLiteDatabase db = NetSQLiteHelper.onOpen();
db.execSQL("update person set person_name=? where _id=?", new String[]{name, id});
db.close();
}
public void upDateByContentValues(String name,String age, String id) {
SQLiteDatabase db = NetSQLiteHelper.onOpen();
ContentValues values = new ContentValues();
values.put("person_name", name);
values.put("person_age", age);
//...
db.update("person", values, "_id=?", new String[]{id});
db.close();
}
public void delBySql() {
SQLiteDatabase db = NetSQLiteHelper.onOpen();
db.execSQL("DELETE FROM person");
db.close();
}
public void delBy() {
SQLiteDatabase db = NetSQLiteHelper.onOpen();
db.delete("person", null, null);
db.close();
}
}
5.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/saveTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:background="@color/colorTv"
android:text="保存数据" />
<TextView
android:id="@+id/queryTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:background="@color/colorTv"
android:text="查询" />
<TextView
android:id="@+id/upDateTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:background="@color/colorTv"
android:text="数据更新" />
<TextView
android:id="@+id/deteDateTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:background="@color/colorTv"
android:text="删除数据" />
<TextView
android:id="@+id/contentTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:background="@color/colorTv"
/>
</LinearLayout>
6.MainActivity
package dome.netsqlite.com.netsqlitedome;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView saveTv, queryTv, upDateTv, contentTv, deteDateTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
saveTv = (TextView) findViewById(R.id.saveTv);//数据保存
saveTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NetSQLiteUitl netSqlUtils = new NetSQLiteUitl();
netSqlUtils.delBy();//此处是防止重复插入数据,出现异常,主键不能重复创建
netSqlUtils.insert("1", "张三", "25", "男");
netSqlUtils.insert("2", "李四", "22", "男");
netSqlUtils.insert("3", "李四", "20", "男");
netSqlUtils.insert("4", "李四", "19", "男");
netSqlUtils.insert("5", "李四", "26", "男");
}
});
queryTv = (TextView) findViewById(R.id.queryTv);//数据查询
queryTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NetSQLiteUitl netSqlUtils = new NetSQLiteUitl();
String str = netSqlUtils.queryAll();
contentTv.setText(str);
}
});
upDateTv = (TextView) findViewById(R.id.upDateTv);//数据更新
upDateTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NetSQLiteUitl netSqlUtils = new NetSQLiteUitl();
netSqlUtils.upDateByContentValues("lisi","13", "1");
}
});
deteDateTv = (TextView) findViewById(R.id.deteDateTv);//删除数据
deteDateTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NetSQLiteUitl netSqlUtils = new NetSQLiteUitl();
netSqlUtils.delBySql();
contentTv.setText("");
}
});
contentTv = (TextView) findViewById(R.id.contentTv);//数据显示
}
}
效果图: