import android.content.Context;
import com.tencent.wcdb.database.SQLiteDatabase;
import com.tencent.wcdb.database.SQLiteOpenHelper;
import java.io.File;
import hisign.com.wcdbdemo.utils.LogUtil;
/**
* 简单的Helper
*/
public class PersonDBHelper extends SQLiteOpenHelper {
// 数据库 db 文件名称
private static final String DEFAULT_NAME = "person.db";
// 默认版本号
private static final int DEFAULT_VERSION = 2;
private Context mContext;
/**
* 通过父类构造方法创建 person 数据库
*/
public PersonDBHelper(Context context) {
super(context, DEFAULT_NAME, null, DEFAULT_VERSION, null);
this.mContext = context;
}
public PersonDBHelper(Context context, byte[] password) {
super(context,DEFAULT_NAME,password,null,DEFAULT_VERSION,null);
this.mContext = context;
}
/**
* 表创建
*/
@Override
public void onCreate(SQLiteDatabase db) {
final String SQL_CREATE = "CREATE TABLE IF NOT EXISTS person (_id INTEGER PRIMARY KEY AUTOINCREMENT , name VARCHAR(20) , address TEXT)";
db.execSQL(SQL_CREATE);
}
/**
* 版本升级
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO
LogUtil.logI("数据库版本"+oldVersion);
LogUtil.logI("数据库新版本"+newVersion);
}
/**
* 删除数据库 db 文件
*/
public boolean onDelete() {
File file = mContext.getDatabasePath(DEFAULT_NAME);
return SQLiteDatabase.deleteDatabase(file);
}
}
import android.content.ContentValues;
import android.content.Context;
import com.tencent.wcdb.Cursor;
import com.tencent.wcdb.database.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
import hisign.com.wcdbdemo.model.Person;
import hisign.com.wcdbdemo.utils.LogUtil;
public class PersonDBManager {
private PersonDBHelper mDBHelper;
private SQLiteDatabase mDB;
public PersonDBManager(Context context) {
// mDBHelper = new PersonDBHelper(context);//没有加密
mDBHelper = new PersonDBHelper(context, "1234".getBytes());//加密
mDB = mDBHelper.getWritableDatabase();
}
/**
* 插入数据
*
* @param person
*/
public boolean addPersonData(Person person) {
try {
// 开启事务
mDB.beginTransaction();
// 执行插入语句
final String sql = "INSERT INTO person VALUES(NULL,?,?)";
Object[] objects = new Object[]{person.getName(), person.getAddress()};
mDB.execSQL(sql, objects);
// 设置事务完成成功
mDB.setTransactionSuccessful();
}catch (Exception e){
return false;
}finally {
// 关闭事务
mDB.endTransaction();
}
return true;
}
/**
* 批量插入数据
*
* @param list
* @return
*/
public boolean addPersonList(List list) {
try {
// 开启事务
mDB.beginTransaction();
// 执行插入语句
for (Person person : list) {
Object[] objects = new Object[]{person.getName(), person.getAddress()};
final String sql = "INSERT INTO person VALUES(NULL,?,?)";
mDB.execSQL(sql, objects);
}
// 设置事务完成成功
mDB.setTransactionSuccessful();
} catch (Exception e) {
return false;
} finally {
// 关闭事务
mDB.endTransaction();
}
return true;
}
/**
* 删除特定姓名的人员数据
* @param name
* @return
*/
public boolean delPersonByName(String name) {
try {
// 开启事务
mDB.beginTransaction();
mDB.delete("person","name=?",new String[]{name});
// 设置事务完成成功
mDB.setTransactionSuccessful();
} catch (Exception e) {
return false;
} finally {
// 关闭事务
mDB.endTransaction();
}
return true;
}
/**
* 通过姓名获取数据库数据
* @param name
* @return
*/
public List getPersonByName(String name){
ArrayList persons = new ArrayList<>();
String sql = "select * from person where name=?";
LogUtil.logI(sql);
Cursor cursor = mDB.rawQuery(sql, new String[]{name});
while (cursor.moveToNext()){
Person person = new Person();
person.setName(cursor.getString(cursor.getColumnIndex("name")));
person.setAddress(cursor.getString(cursor.getColumnIndex("address")));
persons.add(person);
}
return persons;
}
/**
* 获取全部数据库数据
*/
public List getPersonListData() {
List listData = new ArrayList<>();
Cursor c = getAllPersonInfo();
while (c.moveToNext()) {
Person person = new Person();
person.setName(c.getString(c.getColumnIndex("name")));
person.setAddress(c.getString(c.getColumnIndex("address")));
listData.add(person);
}
c.close();
return listData;
}
public boolean updatePersonByName(String name,String address){
ContentValues values = new ContentValues();
values.put("address", address);
int zhuiTao = mDB.update("person", values, "name=?", new String[]{name});
if (zhuiTao > 0) {
return true;
} else {
return false;
}
}
private Cursor getAllPersonInfo() {
return mDB.rawQuery("SELECT * FROM person", null);
}
/**
* 关闭数据库
*/
public void closeDB() {
mDB.close();
}
/**
* 删除数据库文件
*/
public Boolean deleteDatabase() {
return mDBHelper.onDelete();
}
/**
* 删除数据库中所有数据
*/
public void deleteAllData() {
mDB.execSQL("DELETE FROM person;");
}
}
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import hisign.com.wcdbdemo.R;
import hisign.com.wcdbdemo.db.PersonDBManager;
import hisign.com.wcdbdemo.model.Person;
public class PersonActivity extends AppCompatActivity implements View.OnClickListener {
private LinearLayout llSelect;
private LinearLayout llInsert;
private EditText etName;
private Button btnSelect;
private Button btnSelectOk;
private PersonDBManager personDBManager;
private TextView tvSelectResult;
private EditText etInsertName;
private EditText etInsertAdress;
private Button btnInsert;
private Button btnInsertOk;
private LinearLayout llDelete;
private Button btnDeleteSelcet;
private TextView tvDeleteAddress;
private Button btnDeleteOk;
private TextView tvDeleteName;
private LinearLayout llUpdate;
private EditText etUpdateName1;
private Button btnUpdateSelcet;
private TextView tvUpdateName;
private EditText etUpdateAddress;
private Button btnUpdateOk;
private Button btnDelete;
private Button btnUpdate;
private EditText etDeleteName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
personDBManager =new PersonDBManager(PersonActivity.this);
//查询数据
llSelect = (LinearLayout) findViewById(R.id.ll_select);
etName = (EditText) findViewById(R.id.et_name);
tvSelectResult = (TextView) findViewById(R.id.tv_select_result);
btnSelect = (Button) findViewById(R.id.btn_select);
btnSelectOk = (Button) findViewById(R.id.btn_select_ok);
btnSelect.setOnClickListener(this);
btnSelectOk.setOnClickListener(this);
//插入数据
llInsert = (LinearLayout) findViewById(R.id.ll_insert);
etInsertName = (EditText) findViewById(R.id.et_insert_name);
etInsertAdress = (EditText) findViewById(R.id.et_insert_address);
btnInsertOk = (Button) findViewById(R.id.btn_insert_ok);
btnInsert = (Button) findViewById(R.id.btn_insert);
btnInsert.setOnClickListener(this);
btnInsertOk.setOnClickListener(this);
//删除数据
btnDelete = (Button) findViewById(R.id.btn_del);
llDelete = (LinearLayout) findViewById(R.id.ll_delete);
etDeleteName = (EditText) findViewById(R.id.et_delete_name);
btnDeleteSelcet = (Button) findViewById(R.id.btn_delete_selcect);
tvDeleteAddress = (TextView) findViewById(R.id.tv_delete_address);
tvDeleteName = (TextView) findViewById(R.id.tv_delete_name);
btnDeleteOk = (Button) findViewById(R.id.btn_delete_ok);
btnDeleteSelcet.setOnClickListener(this);
btnDeleteOk.setOnClickListener(this);
btnDelete.setOnClickListener(this);
//更新数据
btnUpdate = (Button) findViewById(R.id.btn_update);
llUpdate = (LinearLayout) findViewById(R.id.ll_update);
etUpdateName1 = (EditText) findViewById(R.id.et_update_name1);
btnUpdateSelcet = (Button) findViewById(R.id.btn_update_selcect);
tvUpdateName = (TextView) findViewById(R.id.tv_update_name);
etUpdateAddress = (EditText) findViewById(R.id.et_update_address);
btnUpdateOk = (Button) findViewById(R.id.btn_update_ok);
btnUpdate.setOnClickListener(this);
btnUpdateSelcet.setOnClickListener(this);
btnUpdateOk.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_insert:
llInsert.setVisibility(View.VISIBLE);
llSelect.setVisibility(View.GONE);
llDelete.setVisibility(View.GONE);
llUpdate.setVisibility(View.GONE);
break;
case R.id.btn_insert_ok:
Person person = new Person();
String address = etInsertAdress.getText().toString().trim();
String name = etInsertName.getText().toString().trim();
if (!TextUtils.isEmpty(address)&&!TextUtils.isEmpty(name)){
person.setAddress(address);
person.setName(name);
boolean b = personDBManager.addPersonData(person);
if (b){
Toast.makeText(PersonActivity.this,"插入数据成功!",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(PersonActivity.this,"插入数据失败!",Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(PersonActivity.this,"地址或姓名不能为空!",Toast.LENGTH_LONG).show();
}
break;
case R.id.btn_select:
llInsert.setVisibility(View.GONE);
llDelete.setVisibility(View.GONE);
llUpdate.setVisibility(View.GONE);
llSelect.setVisibility(View.VISIBLE);
break;
case R.id.btn_select_ok:
String strName = etName.getText().toString().trim();
StringBuffer result=new StringBuffer();
if (!TextUtils.isEmpty(strName)) {
List zhuiTaoModels = personDBManager.getPersonByName(strName);
if (zhuiTaoModels != null && zhuiTaoModels.size() != 0) {
for (int i=0;i zhuiTaoModel1 = personDBManager.getPersonByName(nameDel);
if (zhuiTaoModel1.size()!=0){
tvDeleteAddress.setText("地址:"+zhuiTaoModel1.get(0).getAddress());
tvDeleteName.setText("姓名:"+zhuiTaoModel1.get(0).getName());
}else {
Toast.makeText(PersonActivity.this,"查询结果不存在!",Toast.LENGTH_LONG).show();
}
}
break;
case R.id.btn_delete_ok:
String sfzh_del_ok = tvDeleteAddress.getText().toString().trim();
String[] str = tvDeleteName.getText().toString().trim().split(":");
String name_del_ok=str[1];
if (!TextUtils.isEmpty(sfzh_del_ok)&&!TextUtils.isEmpty(name_del_ok)){
boolean b = personDBManager.delPersonByName(name_del_ok);
if (b){
Toast.makeText(PersonActivity.this,"删除数据成功!",Toast.LENGTH_LONG).show();
tvDeleteAddress.setText("");
tvDeleteName.setText("");
}
}
break;
case R.id.btn_update:
llInsert.setVisibility(View.GONE);
llDelete.setVisibility(View.GONE);
llUpdate.setVisibility(View.VISIBLE);
llSelect.setVisibility(View.GONE);
break;
case R.id.btn_update_selcect:
String sfzh_update_select = etUpdateName1.getText().toString().trim();
if (!TextUtils.isEmpty(sfzh_update_select)){
List zhuiTaoModel1= personDBManager.getPersonByName(sfzh_update_select);
if (zhuiTaoModel1.size()!=0){
tvUpdateName.setText("地址:"+zhuiTaoModel1.get(0).getAddress());
etUpdateAddress.setText("姓名:"+zhuiTaoModel1.get(0).getName());
}else {
Toast.makeText(PersonActivity.this,"查询结果不存在!",Toast.LENGTH_LONG).show();
}
}
break;
case R.id.btn_update_ok:
String name_update_ok = tvUpdateName.getText().toString().trim();
String address_update_ok = etUpdateAddress.getText().toString().trim();
if (!TextUtils.isEmpty(address_update_ok)&&!TextUtils.isEmpty(name_update_ok)){
boolean b = personDBManager.updatePersonByName(name_update_ok,address_update_ok);
if (b){
Toast.makeText(PersonActivity.this,"更新数据成功!",Toast.LENGTH_LONG).show();
tvUpdateName.setText("");
etUpdateAddress.setText("");
}
}
break;
default:
}
}
@Override
protected void onDestroy() {
super.onDestroy();
personDBManager.closeDB();
}
}