• android SQlite数据库


    //   通过SQLiteOpenHelper调用如下方法时会返回SQLiteDatabase,通过SQLiteDatabase可以对数据库进行操作
    getWritableDatabase()
    getReadableDatabase()
    public class PersonService {
        private DBOpenHelper dbOpenHelper;
    
        public PersonService(Context context) {
            this.dbOpenHelper = new DBOpenHelper(context);
        }
        
        public void payment(){     通过SQLiteOpenHelper调用如下方法时会返回SQLiteDatabase,通过SQLiteDatabase可以对数据库进行操作
            SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
            db.beginTransaction();//开启事务
            try{
                db.execSQL("update person set amount=amount-10 where personid=1");
                db.execSQL("update person set amount=amount+10 where personid=2");
                db.setTransactionSuccessful();//设置事务的标志为True
            }finally{
                db.endTransaction();//结束事务,有两种情况:commit,rollback,
            //事务的提交或回滚是由事务的标志决定的,如果事务的标志为True,事务就会提交,否侧回滚,默认情况下事务的标志为False
            }
        }
        /**
         * 添加记录
         * @param person
         */
        public void save(Person person){
            SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
            db.execSQL("insert into person(name, phone, amount) values(?,?,?)",
                    new Object[]{person.getName(), person.getPhone(), person.getAmount()});
        }
        /**
         * 删除记录
         * @param id 记录ID
         */
        public void delete(Integer id){
            SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
            db.execSQL("delete from person where personid=?", new Object[]{id});
        }
        /**
         * 更新记录
         * @param person
         */
        public void update(Person person){
            SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
            db.execSQL("update person set name=?,phone=?,amount=? where personid=?",
                    new Object[]{person.getName(), person.getPhone(),  person.getAmount(), person.getId()});
        }
        /**
         * 查询记录
         * @param id 记录ID
         * @return
         */
        public Person find(Integer id){
            SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
            Cursor cursor = db.rawQuery("select * from person where personid=?", new String[]{id.toString()});
            if(cursor.moveToFirst()){
                int personid = cursor.getInt(cursor.getColumnIndex("personid"));
                int amount = cursor.getInt(cursor.getColumnIndex("amount"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String phone = cursor.getString(cursor.getColumnIndex("phone"));
                return new Person(personid, name, phone, amount);
            }
            cursor.close();
            return null;
        }
        /**
         * 分页获取记录
         * @param offset 跳过前面多少条记录
         * @param maxResult 每页获取多少条记录
         * @return
         */
        public List<Person> getScrollData(int offset, int maxResult){
            List<Person> persons = new ArrayList<Person>();
            SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
            Cursor cursor = db.rawQuery("select * from person order by personid asc limit ?,?",
                    new String[]{String.valueOf(offset), String.valueOf(maxResult)});
            while(cursor.moveToNext()){
                int personid = cursor.getInt(cursor.getColumnIndex("personid"));
                int amount = cursor.getInt(cursor.getColumnIndex("amount"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String phone = cursor.getString(cursor.getColumnIndex("phone"));
                persons.add(new Person(personid, name, phone, amount));
            }
            cursor.close();
            return persons;
        }
        /**
         * 分页获取记录
         * @param offset 跳过前面多少条记录
         * @param maxResult 每页获取多少条记录
         * @return
         */
        public Cursor getCursorScrollData(int offset, int maxResult){
            SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
            Cursor cursor = db.rawQuery("select personid as _id,name,phone,amount from person order by personid asc limit ?,?",
                    new String[]{String.valueOf(offset), String.valueOf(maxResult)});
            return cursor;
        }
        
        /**
         * 获取记录总数
         * @return
         */
        public long getCount(){
            SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
            Cursor cursor = db.rawQuery("select count(*) from person", null);
            cursor.moveToFirst();
            long result = cursor.getLong(0);
            cursor.close();
            return result;
        }
    }
    public class OtherPersonService {
        private DBOpenHelper dbOpenHelper;
    
        public OtherPersonService(Context context) {
            this.dbOpenHelper = new DBOpenHelper(context);
        }
        /**
         * 添加记录
         * @param person
         */
        public void save(Person person){
            SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", person.getName());
            values.put("phone", person.getPhone());
            values.put("amount", person.getAmount());
            db.insert("person", null, values);
        }
        /**
         * 删除记录
         * @param id 记录ID
         */
        public void delete(Integer id){
            SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
            db.delete("person", "personid=?", new String[]{id.toString()});
        }
        /**
         * 更新记录
         * @param person
         */
        public void update(Person person){
            SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", person.getName());
            values.put("phone", person.getPhone());
            values.put("amount", person.getAmount());
            db.update("person", values, "personid=?", new String[]{person.getId().toString()});
        }
        /**
         * 查询记录
         * @param id 记录ID
         * @return
         */
        public Person find(Integer id){
            SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
            Cursor cursor = db.query("person", null, "personid=?", new String[]{id.toString()}, null, null, null);
            
            if(cursor.moveToFirst()){
                int personid = cursor.getInt(cursor.getColumnIndex("personid"));
                int amount = cursor.getInt(cursor.getColumnIndex("amount"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String phone = cursor.getString(cursor.getColumnIndex("phone"));
                return new Person(personid, name, phone, amount);
            }
            cursor.close();
            return null;
        }
        /**
         * 分页获取记录
         * @param offset 跳过前面多少条记录
         * @param maxResult 每页获取多少条记录
         * @return
         */
        public List<Person> getScrollData(int offset, int maxResult){
            List<Person> persons = new ArrayList<Person>();
            SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
            Cursor cursor = db.query("person", null, null, null, null, null, "personid asc", offset+ ","+ maxResult);
            
            while(cursor.moveToNext()){
                int personid = cursor.getInt(cursor.getColumnIndex("personid"));
                int amount = cursor.getInt(cursor.getColumnIndex("amount"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String phone = cursor.getString(cursor.getColumnIndex("phone"));
                persons.add(new Person(personid, name, phone, amount));
            }
            cursor.close();
            return persons;
        }
        /**
         * 获取记录总数
         * @return
         */
        public long getCount(){
            SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
            Cursor cursor = db.query("person", new String[]{"count(*)"}, null, null, null, null, null);
            cursor.moveToFirst();
            long result = cursor.getLong(0);
            cursor.close();
            return result;
        }
    }
  • 相关阅读:
    [Intellij] 软件设置和常用快捷键
    [Intellij] Project Structure 配置说明
    [日志log] 常用log日志记录方式对比和详解
    [J2EE]web.xml各个版本模板
    [技术选型] CDH-Cloudera Distribution Hadoop
    [技术选型] dubbo
    [技术选型] spring boot
    [hbase] HBase内置过滤器的一些总结
    [zookeeper] Zookeeper伪分布式集群配置
    [maven] settings 文件 本地maven仓库
  • 原文地址:https://www.cnblogs.com/heml/p/3507791.html
Copyright © 2020-2023  润新知