- package cn.itcast.service;
- 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;
- import cn.itcast.domain.Person;
- 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());
-
- /*第二个参数为空值字段,当第三个参数为null或空时设置,不能设置为personid
- db.execSQL("insert into person(name, phone, amount) values(?,?,?)",
- new Object[]{person.getName(), person.getPhone(), 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()});
- //db.execSQL("delete from person where personid=?", new Object[]{id});
- }
- /**
- * 更新记录
- * @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()});
- //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();
- //第二个参数设为null表示获取虽有数据,等于new String[]{"name"+"phone"+"..."}
- Cursor cursor = db.query("person", null, "personid=?", new String[]{id.toString()}, null, null, null);
- //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.query("person", null, null, null, null, null, "personid asc", offset+ ","+ maxResult);
- //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;
- }
- /**
- * 获取记录总数
- * @return
- */
- public long getCount(){
- SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
- Cursor cursor = db.query("person", new String[]{"count(*)"}, null, null, null, null, null);
- // Cursor cursor = db.rawQuery("select count(*) from person", null);
-
- cursor.moveToFirst();
- long result = cursor.getLong(0);
- cursor.close();
- return result;
- }
- }
复制代码 |
|