码迷,mamicode.com
首页 > 数据库 > 详细

SQLite操作

时间:2015-05-06 14:34:30      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

 1 public class DBOpenHelper extends SQLiteOpenHelper {
 2 
 3     public DBOpenHelper(Context context) {
 4         super(context, "itcast.db", null, 2);//<包>/databases/
 5     }
 6 
 7     @Override
 8     public void onCreate(SQLiteDatabase db) {//是在数据库每一次被创建的时候调用的
 9         db.execSQL("CREATE TABLE person(personid integer primary key autoincrement, name varchar(20), phone VARCHAR(12) NULL)");
10     }
11 
12     @Override
13     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //数据库版本改变时调用
14         db.execSQL("ALTER TABLE person ADD amount integer");
15     }
16 
17 }
  1 public class PersonService {
  2     private DBOpenHelper dbOpenHelper;
  3 
  4     public PersonService(Context context) {
  5         this.dbOpenHelper = new DBOpenHelper(context);
  6     }
  7     
  8     public void payment(){
  9         SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
 10         db.beginTransaction();//开启事务
 11         try{
 12             db.execSQL("update person set amount=amount-10 where personid=1");
 13             db.execSQL("update person set amount=amount+10 where personid=2");
 14             db.setTransactionSuccessful();//设置事务的标志为True
 15         }finally{
 16             db.endTransaction();//结束事务,有两种情况:commit,rollback,
 17         //事务的提交或回滚是由事务的标志决定的,如果事务的标志为True,事务就会提交,否侧回滚,默认情况下事务的标志为False
 18         }
 19     }
 20     /**
 21      * 添加记录
 22      * @param person
 23      */
 24     public void add(Person person){
 25         SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
 26         db.execSQL("insert into person(name, phone, amount) values(?,?,?)",
 27                 new Object[]{person.getName(), person.getPhone(), person.getAmount()});
 28     }
 29     /**
 30      * 删除记录
 31      * @param id 记录ID
 32      */
 33     public void delete(Integer id){
 34         SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
 35         db.execSQL("delete from person where personid=?", new Object[]{id});
 36     }
 37     /**
 38      * 更新记录
 39      * @param person
 40      */
 41     public void update(Person person){
 42         SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
 43         db.execSQL("update person set name=?,phone=?,amount=? where personid=?",
 44                 new Object[]{person.getName(), person.getPhone(),  person.getAmount(), person.getId()});
 45     }
 46     /**
 47      * 查询记录
 48      * @param id 记录ID
 49      * @return
 50      */
 51     public Person find(Integer id){
 52         SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
 53         Cursor cursor = db.rawQuery("select * from person where personid=?", new String[]{id.toString()});
 54         if(cursor.moveToFirst()){
 55             int personid = cursor.getInt(cursor.getColumnIndex("personid"));
 56             int amount = cursor.getInt(cursor.getColumnIndex("amount"));
 57             String name = cursor.getString(cursor.getColumnIndex("name"));
 58             String phone = cursor.getString(cursor.getColumnIndex("phone"));
 59             return new Person(personid, name, phone, amount);
 60         }
 61         cursor.close();
 62         return null;
 63     }
 64     /**
 65      * 分页获取记录
 66      * @param offset 跳过前面多少条记录
 67      * @param maxResult 每页获取多少条记录
 68      * @return
 69      */
 70     public List<Person> getScrollData(int offset, int maxResult){
 71         List<Person> persons = new ArrayList<Person>();
 72         SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
 73         Cursor cursor = db.rawQuery("select * from person order by personid asc limit ?,?",
 74                 new String[]{String.valueOf(offset), String.valueOf(maxResult)});
 75         while(cursor.moveToNext()){
 76             int personid = cursor.getInt(cursor.getColumnIndex("personid"));
 77             int amount = cursor.getInt(cursor.getColumnIndex("amount"));
 78             String name = cursor.getString(cursor.getColumnIndex("name"));
 79             String phone = cursor.getString(cursor.getColumnIndex("phone"));
 80             persons.add(new Person(personid, name, phone, amount));
 81         }
 82         cursor.close();
 83         return persons;
 84     }
 85     /**
 86      * 分页获取记录
 87      * @param offset 跳过前面多少条记录
 88      * @param maxResult 每页获取多少条记录
 89      * @return
 90      */
 91     public Cursor getCursorScrollData(int offset, int maxResult){
 92         SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
 93         Cursor cursor = db.rawQuery("select personid as _id,name,phone,amount from person order by personid asc limit ?,?",
 94                 new String[]{String.valueOf(offset), String.valueOf(maxResult)});
 95         return cursor;
 96     }
 97     
 98     /**
 99      * 获取记录总数
100      * @return
101      */
102     public long getCount(){
103         SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
104         Cursor cursor = db.rawQuery("select count(*) from person", null);
105         cursor.moveToFirst();
106         long result = cursor.getLong(0);
107         cursor.close();
108         return result;
109     }
110 }

 

 

SQLite操作

标签:

原文地址:http://www.cnblogs.com/zhangshan/p/4481559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!