基于Android SQLiteOpenHelper && CRUD 的使用
public class OtherPersonService { private DBOpenHelper dbOpenHelper; public OtherPersonService(Context context) { super(); this.dbOpenHelper = new DBOpenHelper(context, 2); } public void save(Person person) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); ContentValues values = new ContentValues();// ContentValues 专门用于保存字段的值 values.put("name", person.getName()); values.put("phone", person.getPhone()); db.insert("person", null, values);// 第三个参数为字段值,第二个参数为空值字段,如果第三个参数为null } public void delete(Integer id) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); db.delete("person", "personid=?", new String[] { id.toString() }); } public void update(Person person) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", person.getName()); values.put("phone", person.getPhone()); db.update("person", values, "personid=?", new String[] { person.getId().toString() }); } 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")); String name = cursor.getString(cursor.getColumnIndex("name")); String phone = cursor.getString(cursor.getColumnIndex("phone")); return new Person(personid, name, phone); } cursor.close(); return null; } /* * 获取分页数据 */ public List getScollData(int offest, int maxResult) { List persons = new ArrayList(); SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = db.query("person", null, null, null, null, null,"personid asc", offest + "," + maxResult); //db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit); while (cursor.moveToNext()) { int personid = cursor.getInt(cursor.getColumnIndex("personid")); String name = cursor.getString(cursor.getColumnIndex("name")); String phone = cursor.getString(cursor.getColumnIndex("phone")); persons.add(new Person(personid, name, phone)); } cursor.close(); return persons; } public long getCount() { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = db.query("person", new String[] { "count(*)" }, null, null, null, null, null); //db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy); cursor.moveToFirst(); long result = cursor.getLong(0); return result; }}
- 【Hibernate框架开发之七】Hibernate使用Annotation中各种关系映射的CRUD(增删改查)&&集合映射&&继承映射
- 【Hibernate框架开发之七】Hibernate使用Annotation中各种关系映射的CRUD(增删改查)&&集合映射&&继承映射
- C++ FFLIB 之FFDB: 使用 Mysql&Sqlite 实现CRUD
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- iOS core Data 详解-<1>基本使用
- Core Graphics使用 >> 绘制心电图
- >/dev/null 2>&1 这句话的含义及使用的意义
- 20160403_联合体的本质和使用 & 与struct的不同 & 大端存储和小端存储
- cocos2d-x 使用 cocostu…
- argc & argv的使用
- 使用INPUT结构时出现“error C2061: syntax error : identifier 'INPUT'”
- HTML5 & CSS3初学者指南(4) – Canvas使用
- 彩色的命令行 —— 使用 ANSI 色彩代码(export PS1='\[\e[1;32m\][\u@\h \w]\$\[\e[0m\] ')
- Git的基本使用方法和安装&心得体会(使用git命令行)
- 解决dev express quantumgrid v6.30使用中出现'Overflow while converting variant of type(Integer) into type(Date)'的错误
- 解决SqlServer "设备激活错误,请使用 WITH MOVE 选项来标识该文件的有效位置"
- java.lang.Integer.toHexString(b[n] & 0XFF)中0XFF使用的必要性
- pip&conda使用清华源
- 在Oracle 9i/10g中使用Flashback
- [Unity&]PlayerPrefs.GetString的使用案例