Android SQLite数据缓存操作

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBOpenHelper extends SQLiteOpenHelper {

	public DBOpenHelper(Context context) {
		super(context, "people.db", null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		//创建人员表
		db.execSQL("drop table if exists people");
		db.execSQL("create table people (id integer primary key autoincrement,name varchar(64),sex smallint,contact varchar(64))");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

}

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.text.people.People;
import com.text.people.StrUtil;

public class PeopleService {
	private DBOpenHelper dbOpenHelper;
	private static PeopleService instance = null;

	public PeopleService(Context context) {
		this.dbOpenHelper = new DBOpenHelper(context);
	}

	public synchronized static PeopleService getInstance(Context ctx) {
		if (null == instance) {
			instance = new PeopleService(ctx);
		}
		return instance;
	}
	
	//增加数据
	public void save(People people) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("name", people.getName());
		values.put("sex", people.getSex());
		values.put("contact", people.getContact());
		db.insert("people", null, values);
	}

	//根据id删除日志
	public void delete(Integer id) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.delete("people", "id=?", new String[] { id.toString() });
	}

	//删除所有数据
	public void deleteAll() {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.delete("people", "", null);
	}

	//根据id修改数据
	public void update(People people) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("name", people.getName());
		values.put("sex", people.getSex());
		values.put("contact", people.getContact());
		db.update("people", values, "id=?", new String[] { people.getId().toString() });
	}

	//根据id查询单条数据
	public People find(Integer id) {
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.query("people",new String[] { "id,name,sex,contact" },
						"id=?", new String[] { id.toString() }, null, null,
						null, "1");
		try {
			if (cursor.moveToFirst()) {
				People people = new People();
				people.setId(id);
				people.setName(cursor.getString(cursor.getColumnIndex("name")));
				people.setSex(cursor.getInt(cursor.getColumnIndex("sex")));
				people.setContact(cursor.getString(cursor.getColumnIndex("contact")));
				return people;
			}
			return null;
		} finally {
			cursor.close();
		}
	}

	//分页加载数据
	public List<People> getScrollData(int currentPage, int lineSize,String whereStr) {
		String firstResult = String.valueOf((currentPage - 1) * lineSize);
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = null;
		try {
			if(StrUtil.isNull(whereStr)){
				cursor = db.query("people",
						new String[] { "id,name,sex,contact" },
						null, null, null, null, "id desc",
						firstResult + "," + lineSize);
			}else{
				cursor = db.query("people",
						new String[] { "id,name,sex,contact" },
						"name like ? or contact like ?", new String[]{"%"+whereStr+"%","%"+whereStr+"%"}, null, null, "id desc",
						firstResult + "," + lineSize);
			}
			List<People> peoples = new ArrayList<People>();
			while (cursor.moveToNext()) {
				People people = new People();
				people.setId(cursor.getInt(cursor.getColumnIndex("id")));
				people.setName(cursor.getString(cursor.getColumnIndex("name")));
				people.setSex(cursor.getInt(cursor.getColumnIndex("sex")));
				people.setContact(cursor.getString(cursor.getColumnIndex("contact")));
				peoples.add(people);
			}
			return peoples;
		} finally {
			if(cursor!=null){
				cursor.close();
			}
		}
	}

	//查询总记录数
	public int getCount() {
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select count(1) from people", null);
		try {
			cursor.moveToFirst();
			return cursor.getInt(0);
		} finally {
			cursor.close();
		}
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农-004

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值