SQLiteDatabase还专门提供了对应于添加、删除、更新、查询的操作方法:insert()、delete()、update()、和query()。
insert()方法用于添加数据,各个字段的数据使用ContentValues进行存放。
ContentValues类似于map,相对于map,它提供了存取数据对应的put(String key,xxx Value)和getAsXxx(String key)方法,key为字段名称,value为字段值,Xxx指的是各种常用的数据类型,如:String、integet等。
insert添加
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.domain.Person;
import java.util.ArrayList;
import java.util.List;
public class OtherpersonService {
private Context context;
private DBOpenHelper dbOpenHelper;
public OtherpersonService(Context context,DBOpenHelper dbOpenHelper) {
this.context=context;
this.dbOpenHelper=dbOpenHelper;
}
//添加
public void save(Person person){
SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
ContentValues values=new ContentValues();
//name一定要和表的名称一致
values.put("name",person.getName());
values.put("phone",person.getPhone());
//第一个参数是表的名称
//第二个参数为空值字段,就是如果第三个参数为空(null)的时候就会用到第二个参数的值。用第二个参数代替第三个参数组拼成SQL语句
//比如:insert into person(name) values(null) 这里的person字段使用了第二个参数的name
//第三个参数不为空就不会用到第二个参数
db.insert("person","name",values);//values值为null就使用
}
delete删除
//删除
public void delete(Integer id){
SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
//1表名、2字段名、3占位符的数据
db.delete("person","personid=?",new String[]{id.toString()});
}
update更新
ContentValues类似于map,相对于map,它提供了存取数据对应的put(String key,xxx Value)和getAsXxx(String key)方法,key为字段名称,value为字段值,Xxx指的是各种常用的数据类型,如:String、integet等。
//更新
public void update(Person person){
SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",person.getName());
values.put("phone",person.getPhone());
//1表名、2需要更新值、3以什么条件字段更新、4条件字段的数据值(占位符的值)
db.update("person",values,"personid=?",new String[]{person.getId().toString()});
}
query查询
//查询
public Person find(Integer id){
SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
//1 表名、 2 需要查询的字段列表,用字符串数组形式传入,null为所有的字段、 3 以什么条件字段查询、 4 条件字段的数据值(占位符的值)、
// 5 groupBy相当于select语句的groupby后面的部分、 6 having相当于select语句的having后面的部分、 7 order是我们想要的排序方式。
Cursor cursor=db.query("person",null,"personid=?",new String[]{id.toString()},null,null,null);
//moveToFirst移动一下,判断是否可以移动
if(cursor.moveToFirst()){
Integer personid=cursor.getInt(0);
String name=cursor.getString(1);
String phone=cursor.getString(2);
return new Person(personid,name,phone);
}
return null;
}
query分页查询
//分页查询
public List<Person> getScrolld(int offset, int maxResult){
SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
List<Person> personList=new ArrayList<>();
// Cursor cursor=db.rawQuery("select * from person order by personid asc limit ?,?",new String[]{String.valueOf(offset),String.valueOf(maxResult)});
//"personid asc",offset+","+maxResult这段相当于这里personid asc limit ?,?",new String[]{String.valueOf(offset),String.valueOf(maxResult)}
Cursor cursor=db.query("person",null,null,null,null,null,"personid asc",offset+","+maxResult);
while (cursor.moveToNext()){
int personid=cursor.getInt(0);
String name=cursor.getString(1);
String phone=cursor.getString(2);
personList.add(new Person(personid,name,phone));
}
db.close();
return personList;
}
count总数
//总数
public int getCount(){
SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
//select count(*) from person",null
//new一个字符串
Cursor cursor = db.query("person", new String[]{"count(*)"}, null, null, null, null,null);
cursor.moveToFirst();
int result=cursor.getInt(0);
db.close();
return result;
}
}