SQLite对于数据库的操作

前端之家收集整理的这篇文章主要介绍了SQLite对于数据库的操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sqlite对于数据库的操作,在android中已经对于它的增、删、改、查、取得所有的数据数以及分页操作有着自定义方法,如下:

public void add(Person person) {
	sqliteDatabase db = helper.getWritableDatabase();
	ContentValues values = new ContentValues();
	values.put("name",person.getName());
	values.put("age",person.getAge());
	values.put("phone",person.getPhone());
	
	db.insert("person","name,age,phone",values);
}
	
public void delete(Integer id){
	sqliteDatabase db = helper.getWritableDatabase();
	db.delete("person","id=?",new String[]{id.toString()});
}
	
public void update(Person person) {
	sqliteDatabase db = helper.getWritableDatabase();
		
	ContentValues values = new ContentValues();
	values.put("name",person.getPhone());
		
	db.update("person",values,new String[]{person.getId().toString()});
}
	
public Person query(Integer id){
	sqliteDatabase db = helper.getWritableDatabase();
	Cursor cursor = db.query("person",null,new String[]{id.toString()},null);
	if (cursor.moveToFirst()) {
		int personid = cursor.getInt(cursor.getColumnIndex("id"));
		String name = cursor.getString(cursor.getColumnIndex("name"));
		String phone = cursor.getString(cursor.getColumnIndex("phone"));
		short age = cursor.getShort(cursor.getColumnIndex("age"));
		if(cursor != null)
			cursor.close();
		return new Person(personid,name,phone,age);
	}
	return null;
}
	
public int getCount(){
	sqliteDatabase db = helper.getReadableDatabase();
	Cursor cursor = db.query("person",new String[]{"count(*)"},null);
	cursor.moveToFirst();
	int count = cursor.getInt(0);
		
	if(cursor != null)
		cursor.close();
		
	return count;
}
	
public List<Person> getLimitData(int offset,int maxCount) {
	List<Person> persons = new ArrayList<Person>();
		
	sqliteDatabase db = helper.getReadableDatabase();
	Cursor cursor = db.query("person",offset + "," + maxCount);
	while(cursor.moveToNext()){
		int personid = cursor.getInt(cursor.getColumnIndex("id"));
		String name = cursor.getString(cursor.getColumnIndex("name"));
		String phone = cursor.getString(cursor.getColumnIndex("phone"));
		short age = cursor.getShort(cursor.getColumnIndex("age"));
		persons.add(new Person(personid,age));
	}
		
	if(cursor != null){
		cursor.close();
	}
	return persons;
}
原文链接:https://www.f2er.com/sqlite/201331.html

猜你在找的Sqlite相关文章