SQlite的数据库查询
多表查询 + 数据字典查询
数据字典
1 select b.codedesc,a.[branum],d.codedesc,c.codedesc 2 from BI_VEHICLE a 3 join T_PF_DATADICT b on b.fieldname = \'BRAPREFIX\' and a.braprefix = b.code 4 join T_PF_DATADICT c on c.fieldname = \'VECTYPE\' and a.vectype = c.code 5 join T_PF_DATADICT d on d.fieldname = \'TRASTATE\' and a.trastate = d.code
把多个字段结果合成一个字段
1 select b.codedesc as _id , 2 a.[casecarnum], 3 a.[credate]||" "||a.[cretime] as credate, 4 a.[edidate]||" "||a.[editime] as edidate , 5 a.[pecdate]||" "||a.[pectime] as pecdate, 6 a.[casereason], a.[client_status], c.[codedesc] as status 7 from cp_casinfo a 8 join T_PF_DATADICT b on b.fieldname = \'BRAPREFIX\' and a.casecarprefix = b.code 9 join T_PF_DATADICT c on c.[fieldname] = \'STATUS\' and a.[status] = c.[code] and c.tablename = \'CP_CASEINFO\'
原字段
合并后的字段
———————————————————————————————————————————————-
分页查询数据
1 /** 2 * 分页获取记录 3 * @param offset 跳过前面多少条记录 4 * @param maxResult 每页获取多少条记录 5 * @return 6 */ 7 public List<Person> getScrollData(int offset, int maxResult) 8 { 9 10 List<Person> persons = new ArrayList<Person>(); 11 SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); 12 Cursor cursor = db.rawQuery("select * from person order by personid asc limit ?,?", 13 new String[]{String.valueOf(offset), String.valueOf(maxResult)}); 14 15 while(cursor.moveToNext()) 16 { 17 int personid = cursor.getInt(cursor.getColumnIndex("personid")); 18 int amount = cursor.getInt(cursor.getColumnIndex("amount")); 19 String name = cursor.getString(cursor.getColumnIndex("name")); 20 String phone = cursor.getString(cursor.getColumnIndex("phone")); 21 persons.add(new Person(personid, name, phone, amount)); 22 } 23 cursor.close(); 24 return persons; 25 }
数据库的增删改查
1 //方法一 2 ContentValues cv = new ContentValues(); 3 cv.put("CLASS_CODE", clazz.getClassCode()); 4 cv.put("CLASS_NAME", clazz.getClassName()); 5 //返回自增列的 ID 值 6 long lResult = sdb.insert("tableName", null, cv); 7 8 9 //方法二 10 // 11 SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); 12 db.execSQL("insert into person(name, phone, amount) values(?,?,?)", 13 new Object[]{person.getName(), person.getPhone(), person.getAmount()});
1 //方法一 2 int iResult = sdb.delete(DBConstant.TN_CLASS, "ID=?", new String[]{String.valueOf(clazz.getId())}); 3 4 //方法二 5 /** 6 * 删除记录 7 * @param id 记录ID 8 */ 9 public void delete(Integer id){ 10 SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); 11 db.execSQL("delete from person where personid=?", new Object[]{id}); 12 }
1 //方法一 2 ContentValues cv = new ContentValues(); 3 cv.put("CLASS_CODE", clazz.getClassCode()); 4 cv.put("CLASS_NAME", clazz.getClassName()); 5 int iResult = sdb.update(DBConstant.TN_CLASS, cv, "ID=?", new String[]{String.valueOf(clazz.getId())}); 6 7 /** 8 * 更新记录 9 * @param person 10 */ 11 public void update(Person person){ 12 SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); 13 db.execSQL("update person set name=?,phone=?,amount=? where personid=?", 14 new Object[]{person.getName(), person.getPhone(), person.getAmount(), person.getId()}); 15 }
1 /** 2 * 查询记录 3 * @param id 记录ID 4 * @return 5 */ 6 public Person find(Integer id){ 7 SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); 8 Cursor cursor = db.rawQuery("select * from person where personid=?", new String[]{id.toString()}); 9 if(cursor.moveToFirst()){ 10 int personid = cursor.getInt(cursor.getColumnIndex("personid")); 11 int amount = cursor.getInt(cursor.getColumnIndex("amount")); 12 String name = cursor.getString(cursor.getColumnIndex("name")); 13 String phone = cursor.getString(cursor.getColumnIndex("phone")); 14 return new Person(personid, name, phone, amount); 15 } 16 cursor.close(); 17 return null; 18 }
版权声明:本文为totome原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。