前端之家收集整理的这篇文章主要介绍了
参数拼接方式实现SQLite数据库的增删改查,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public class OtherPersonService {
private DBOpenHelper dbOpenHelper;
public OtherPersonService(Context context){
dbOpenHelper = new DBOpenHelper(context);
}
public void save(Person person){
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",person.getName());
values.put("phone",person.getPhone());
values.put("amount",person.getAmount());
db.insert("person",null,values);//insert into person(personid) values(NULL)
}
public void update(Person person){
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",person.getAmount());
db.update("person",values,"personid=?",new String[]{person.getId().toString()});
}
public Person find(Integer id){
sqliteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("person",new String[]{id.toString()},null);
if(cursor.moveToFirst()){
int personid = cursor.getInt(cursor.getColumnIndex("personid"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
cursor.close();
return new Person(personid,name,phone,amount);
}
return null;
}
public void delete(Integer id){
sqliteDatabase db = dbOpenHelper.getWritableDatabase();
db.delete("person",new String[]{id.toString()});
}
public List<Person> getScrollData(int offset,int maxResult){
List<Person> persons = new ArrayList<Person>();
sqliteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("person","personid asc",offset+","+ maxResult);
while(cursor.moveToNext()){
int personid = cursor.getInt(cursor.getColumnIndex("personid"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
persons.add(new Person(personid,amount));
}
cursor.close();
return persons;
}
public long getCount(){
sqliteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("person",new String[]{"count(*)"},null);
cursor.moveToFirst();
return cursor.getLong(0);
}
}
原文链接:https://www.f2er.com/sqlite/200881.html