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;
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。