- 封裝的InfoDao類(數(shù)據(jù)的封裝央拖,增刪改查)
public boolean add(Context context,InfoBean bean){
//由于每個(gè)方法都需要?jiǎng)?chuàng)建數(shù)據(jù)庫(kù),提取出來(lái)
//MysqliteOpenHelper mysqliteOpenHelper = new MysqliteOpenHelper(context);
SQLiteDatabase db = mysqliteOpenHelper.getReadableDatabase();
//table:表名 nullColumnHack values:數(shù)據(jù)一行的值
ContentValues values = new ContentValues();//用map封裝的對(duì)象,用來(lái)存放值
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封裝的對(duì)象售葡,用來(lái)存放值
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語(yǔ)句 selectionArgs占位符的值
Cursor cursor = db.rawQuery("select * from info where name=?",new String[]{name} );
//解析cursor的數(shù)據(jù)
if(cursor !=null && cursor.getCount()>0){//是否存在數(shù)據(jù)
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 查詢條件參數(shù) 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){//是否存在數(shù)據(jù)
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函數(shù)中創(chuàng)建info.db
- 在MainActivity中實(shí)現(xiàn)相應(yīng)操作
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;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者