sqlite增删改查(二)

使用sqlite自带的crud方法操作数据库,不赞成使用,因为效率没直接操作sql语句高

所以只把例子贴出来

OtherPersonService
package com.tjp.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 com.tjp.model.Person;

public class OtherPersonService {

private DBOpenHelp openHelp=null;
private Context context;


public OtherPersonService(Context context) {
this.context = context;
openHelp
=new DBOpenHelp(context);
}

public void save(Person person){
SQLiteDatabase database
= openHelp.getWritableDatabase();//对数据要更改,该方法以读和写的方式打开数据库,有缓存功能
//database.execSQL("insert into person(name) values(?)",new Object[]{person.getName()});
ContentValues values=new ContentValues();
values.put(
"name", person.getName());
database.insert(
"person", null, values);//不管有没有数据都想数据库里面保存数据,
//database.insert("person", "name", null);//如果没有给定第三个字段或第三个参数个数为0个,那个就要指定一个空值字段
}

public void update(Person person){
SQLiteDatabase database
= openHelp.getWritableDatabase();
//database.execSQL("update person set name=? where personid=?",new Object[]{person.getName(),person.getPersonId()});
ContentValues values=new ContentValues();
values.put(
"name", person.getName());
database.update(
"person", values, "personid=?", new String[]{String.valueOf(person.getPersonId())});
}

public void delete(int personId){
SQLiteDatabase database
= openHelp.getWritableDatabase();
//database.execSQL("delete from person where personid=?",new Object[]{personId});
database.delete("person", "personid=?", new String[]{String.valueOf(personId)});
}

public Person find(int personId){
SQLiteDatabase database
= openHelp.getWritableDatabase();
Person person
=null;
//Cursor cursor= database.rawQuery("select * from person where personid=?", new String[]{String.valueOf(personId).toString()});
Cursor cursor=database.query("person", new String[]{"personid","name"},
"personid=?", new String[]{String.valueOf(personId)}, null, null, null);
if(cursor.moveToFirst()){//如果移动成功了表示存在
int personIda =cursor.getInt(cursor.getColumnIndex("personid"));
String name
=cursor.getString(cursor.getColumnIndex("name"));
person
=new Person(personIda,name);
}
cursor.close();
return person;
}

public List<Person> getScrollDate(int offerset,int maxResult){
List
<Person> persons=new ArrayList<Person>();
SQLiteDatabase database
= openHelp.getWritableDatabase();
//String sql="select * from person limit ?,?";
//Cursor cursor= database.rawQuery(sql, new String[]{String.valueOf(offerset),String.valueOf(maxResult)});

Cursor cursor
=database.query("person", null, null, null, null, null, null, offerset+","+maxResult);
while(cursor.moveToNext()){
int personIda =cursor.getInt(cursor.getColumnIndex("personid"));
String name
=cursor.getString(cursor.getColumnIndex("name"));
Person person
=new Person(personIda,name);
persons.add(person);
}
return persons;
}

public long getCount(){
SQLiteDatabase database
= openHelp.getWritableDatabase();
//Cursor cursor= database.rawQuery("select count(*) from person", null);
Cursor cursor=database.query("person", new String[]{"count(*)"}, null, null, null, null, null);
cursor.moveToFirst();
int count=cursor.getInt(0);
cursor.close();
return count;
}
}

当数据库更新的时候只要把版本号更新就会执行onUpgrade

posted @ 2011-05-23 22:40  飞鹰小谭  阅读(420)  评论(0编辑  收藏  举报