找传奇、传世资源到传世资源站!

android Sqlite 分页 示例源码(入门级)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

测试数据库的分页操作,便于节省手机内存,适用初学者
from clipboard package com.android.service;import java.util.ArrayList;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import com.android.database.DBHelper;/** * * @author shangzhenxiang * */public class DatabaseService {private Context mContext;private DBHelper dbHelper;public DatabaseService(Context context) {// TODO Auto-generated constructor stubmContext = context;dbHelper = new DBHelper(mContext);}//添加public void insert(String title) {SQLiteDatabase db = dbHelper.getWritableDatabase();String sql = "insert into database(title) values(?)";db.execSQL(sql, new String[]{title});}//删除public void delete(String title) {SQLiteDatabase db = dbHelper.getWritableDatabase();String sql = "delete from database where title = ?";db.execSQL(sql, new String[]{title});}//查找public ArrayList<String> find(int id) {SQLiteDatabase db = dbHelper.getWritableDatabase();String sql = "select * from database where _id = ? ";Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});ArrayList<String> titles = new ArrayList<String>();if(c.moveToNext()) {String title =c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_TITLE));titles.add(title);return titles;}//不用忘记关闭Cursor。c.close();return null;}//更新public void upDate(int id, String title) {SQLiteDatabase db = dbHelper.getWritableDatabase();String sql = "update database set title =? where _id = ?";db.execSQL(sql, new String[]{String.valueOf(title), String.valueOf(id)});}//查询记录的总数public long getCount() {SQLiteDatabase db = dbHelper.getWritableDatabase();String sql = "select count(*) from database";Cursor c = db.rawQuery(sql, null);c.moveToFirst();long length = c.getLong(0);c.close();return length;}/** * 拿到所有的记录条数 * @param firstResult 从第几条数据开始查询。 * @param maxResult 每页显示多少条记录。 * @return 当前页的记录 */public Cursor getAllItems(int firstResult, int maxResult) {SQLiteDatabase db = dbHelper.getWritableDatabase();String sql = "select * from database limit ?,?";Cursor mCursor = db.rawQuery(sql, new String[]{String.valueOf(firstResult), String.valueOf(maxResult)});return mCursor;}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复