Sqlite数据库操作Dao类

public class UserDao {


private SQLiteDatabase sd;
public UserDao(Context context) {
Sqlite sqlite=new Sqlite(context);
sd=sqlite.getWritableDatabase();
}
//加数据
/**
* 1.表名
* 2.列的信息 是否存在
* 3.要存的值
*/
public boolean add(){
ContentValues values=new ContentValues();
values.put("name", "封不觉");
values.put("money", "500000");
long insert=sd.insert("user", null, values);
if(insert!=-1){
return true;
}else{
return false;
}
}
//删除数据
/**
* 1.表名
* 2.条件
* 3.条件的值
*/
public boolean delete(){

int delete = sd.delete("user", "id=?", new String[]{1+""});
if(delete!=-1){
return true;
}else{
return false;
}
}
public boolean delete1(int id){

int delete = sd.delete("user", "id=?", new String[]{id+""});
if(delete!=-1){
return true;
}else{
return false;
}
}
//修改数据
/**
* 1.表名
* 2.要修改的值
* 3.条件  要修改那一条数据
* 4.条件的值
*/
public boolean update(){
ContentValues values=new ContentValues();
values.put("name", "枉叹之");
values.put("money", "2000000");

int update = sd.update("user", values, "id=?", new String[]{4+""});
if(update!=-1){
return true;
}else{
return false;
}
}
public boolean update1(String name,String money,int id){
ContentValues values=new ContentValues();
values.put("name", name);
values.put("money", money);

int update = sd.update("user", values, "id=?", new String[]{id+""});
if(update!=-1){
return true;
}else{
return false;
}
}
//查找一条数据
/**
* 1.表名
* 2.指定查询的列字段
* 3.条件
* 4.条件的值
* 5.组查询
* 6.子查询
* 7.顺序   升序 and  降序
*/
public List select_one(){
List list=new ArrayList();

Cursor cursor=sd.query("user", null, "id=?", new String[]{2+""}, null, null, null);
while(cursor.moveToNext()){
int id=cursor.getInt(cursor.getColumnIndex("id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
String money=cursor.getString(cursor.getColumnIndex("money"));

User user=new User(name, money, id);

list.add(user);
}
return list;
}
//查询全部数据
public List select_all(){
List list=new ArrayList();
Cursor cursor=sd.query("user", null, null, null, null, null, null);
while(cursor.moveToNext()){
int id=cursor.getInt(cursor.getColumnIndex("id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
String money=cursor.getString(cursor.getColumnIndex("money"));
User user=new User(name, money, id);
list.add(user);
}
return list;
}

}

你可能感兴趣的:(Sqlite数据库操作Dao类)