public boolean add(Context context,InfoBean bean){
//由于每个方法都需要创建数据库,提取出来
//MysqliteOpenHelper mysqliteOpenHelper = new MysqliteOpenHelper(context);
SQLiteDatabase db = mysqliteOpenHelper.getReadableDatabase();
//table:表名 nullColumnHack values:数据一行的值
ContentValues values = new ContentValues();//用map封装的对象,用来存放值
values.put("name", bean.name);
values.put("phone", bean.phone);
long insert_result = db.insert("info", null, values);
db.close();
if(insert_result !=-1){
return true;
}else{
return false;
}
}
public int del(String name){
SQLiteDatabase db = mysqliteOpenHelper.getReadableDatabase();
//db.delete(table, whereClause, whereArgs)
int result_del = db.delete("info","name=?",new String[]{name});
db.close();
return result_del;
}
public int update(InfoBean bean){
SQLiteDatabase db = mysqliteOpenHelper.getReadableDatabase();
//db.update(table, values, whereClause, whereArgs)
ContentValues values = new ContentValues();//用map封装的对象,用来存放值
values.put("phone", bean.phone);//只更新phone
int result_update = db.update("info", values, "name=?",new String[]{bean.name} );
db.close();
return result_update;
}
/* public void query(String name){
SQLiteDatabase db = mysqliteOpenHelper.getReadableDatabase();
//execSQL没有返回值,不适合做查询操作
//sql:sql语句 selectionArgs占位符的值
Cursor cursor = db.rawQuery("select * from info where name=?",new String[]{name} );
//解析cursor的数据
if(cursor !=null && cursor.getCount()>0){//是否存在数据
while(cursor.moveToNext()){
int _id = cursor.getInt(0);
String name_str = cursor.getString(1);
String phone = cursor.getString(2);
System.out.println("_id:"+_id+";name:"+name_str+";phone:"+phone);
}
}
}*/
public void query(String name){
SQLiteDatabase db = mysqliteOpenHelper.getReadableDatabase();
/*
* table: columns:查询的列名,为null查询所有列 selection:查询条件
* selectionArgs 查询条件参数 groupBy:按什么分组 having 分组的条件 orderBy:按什么排序
* */
Cursor cursor = db.query("info", new String[]{"_id","name","phone"}, "name=?",
new String[]{name}, null, null, null);
if(cursor !=null && cursor.getCount()>0){//是否存在数据
while(cursor.moveToNext()){
int _id = cursor.getInt(0);
String name_str = cursor.getString(1);
String phone = cursor.getString(2);
System.out.println("_id:"+_id+";name:"+name_str+";phone:"+phone);
}
}
}
- 在MysqliteOpenHelper类的onCreate函数中创建info.db
- 在MainActivity中实现相应操作
public void onClick(View v) {
InfoDao infoDao = new InfoDao(mContext);
switch (v.getId()) {
case R.id.bt_add:
InfoBean bean = new InfoBean ();
bean.name="Kavin";
bean.phone="100";
boolean result = infoDao.add(mContext, bean);
if(result){
Toast.makeText(mContext, "添加成功", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(mContext, "添加成功", Toast.LENGTH_SHORT).show();
}
break;
case R.id.bt_del:
int result_del = infoDao.del("Kavin");
Toast.makeText(mContext, "成功删除"+result_del+"条信息", Toast.LENGTH_SHORT).show();
break;
case R.id.bt_update:
InfoBean bean2 = new InfoBean ();
bean2.name="Kavin";
bean2.phone="100000000";
int result_update = infoDao.update(bean2);
Toast.makeText(mContext, "成功更新"+result_update+"条信息", Toast.LENGTH_SHORT).show();
break;
case R.id.bt_query:
infoDao.query("Kavin");
infoDao.query("Aris");
break;
}