注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

姑射道人的博客

博客新地址:nixuchen.com

 
 
 

日志

 
 

SQlite  

2012-11-20 14:36:39|  分类: android |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
package cn.itcast.sqlite;

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;

public class PersonDao {
        private DBOpenHelper helper;

        public PersonDao(Context context) {
                helper = new DBOpenHelper(context);
        }
        
        public void remit(int from, int to, int amount) {
                SQLiteDatabase db = helper.getWritableDatabase();        
                
                // 开启事务
                db.beginTransaction();
                try {
                        db.execSQL("UPDATE person SET balance=balance-? WHERE id=?", new Object[]{amount, from});
                        db.execSQL("UPDATE person SET balance=balance+? WHERE id=?", new Object[]{amount, to});
                        db.setTransactionSuccessful();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                // 结束事务, 将事务成功点前面的代码提交
                db.endTransaction();
                
                db.close();
        }

        public void insert(Person p) {
                SQLiteDatabase db = helper.getWritableDatabase();        
                
                // 准备数据
                ContentValues values = new ContentValues();
                values.put("name", p.getName());
                values.put("balance", p.getBalance());
                
                // 通过ContentValues中的数据拼接SQL语句, 执行插入操作, id为表中的一个列名
                db.insert("person", "id", values);                
                
                db.close();
        }

        public void delete(int id) {
                SQLiteDatabase db = helper.getWritableDatabase();
                
                // 执行删除操作, 在person表中删除id为指定值的记录
                db.delete("person", "id=?", new String[]{String.valueOf(id)});
                
                db.close();
        }

        public void update(Person p) {
                SQLiteDatabase db = helper.getWritableDatabase();

                // 要更新的数据
                ContentValues values = new ContentValues();
                values.put("name", p.getName());
                values.put("balance", p.getBalance());

                // 更新person表中id为指定值的记录
                db.update("person", values, "id=?", new String[] { String.valueOf(p.getId()) });
                
                db.close();
        }

        public Person query(int id) {
                SQLiteDatabase db = helper.getReadableDatabase();
                
                // 执行查询: 不去重复, 表是person, 查询name和balance两列, Where条件是"id=?", 占位符是id, 不分组, 没有having, 不排序, 没有分页
                Cursor c = db.query(false, "person", new String[]{"name", "balance"}, "id=?", new String[]{String.valueOf(id)}, null, null, null, null);
                
                Person p = null;
                // 判断Cursor是否有下一条记录
                if (c.moveToNext())
                        // 从Cursor中获取数据, 创建Person对象
                        p = new Person(id, c.getString(0), c.getInt(1));
                
                // 释放资源
                c.close();
                db.close();
                return p;
        }

        public List<Person> queryAll() {
                SQLiteDatabase db = helper.getReadableDatabase();

                // 查询所有记录, 倒序
                Cursor c = db.query(false, "person", new String[]{"id","name", "balance"}, null, null, null, null, "id DESC", null);
                
                List<Person> persons = new ArrayList<Person>();
                while (c.moveToNext())
                        persons.add(new Person(c.getInt(0), c.getString(1), c.getInt(2)));
                c.close();
                db.close();
                return persons;
        }
        
        public List<Person> queryPage(int pageNum, int capacity) {                
                // 开始索引
                String start = String.valueOf((pageNum - 1) * capacity);
                // 查询的个数
                String length = String.valueOf(capacity);
                
                SQLiteDatabase db = helper.getReadableDatabase();
                
                // 翻页查询
                Cursor c = db.query(false, "person", new String[]{"id","name", "balance"}, null, null, null, null, null, start + "," + length);
                
                List<Person> persons = new ArrayList<Person>();
                while (c.moveToNext())
                        persons.add(new Person(c.getInt(0), c.getString(1), c.getInt(2)));
                c.close();
                db.close();
                return persons;
        }
        
        public int queryCount() {
                SQLiteDatabase db = helper.getReadableDatabase();
                
                // 查询记录条数
                Cursor c = db.query(false, "person", new String[]{"COUNT(*)"}, null, null, null, null, null, null);
                
                c.moveToNext();
                int count =c.getInt(0);
                c.close();
                db.close();
                return count;
        }

}
  评论这张
 
阅读(877)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017