sqlite 数据库的操作
********创建*********** import android.content.Context; public class DataBaSEOpenHelper extends SQLiteOpenHelper { } ********操作*********** import java.util.ArrayList; import android.database.Cursor; public class DataBaseService {private DataBaSEOpenHelper opendb;private SQLiteDatabase db ;public DataBaseService(DataBaSEOpenHelper opendb) {this.opendb = opendb;db = this.opendb.getReadableDatabase();}public void save(Person person) {db.execSQL("insert into person(name,age) values(?,?)",new Object[]{person.getName(),person.getAge()});}public void delete(Integer id) {db.execSQL("delete from person where personid=?",new Object[]{id});}public void update(Person person) {db.execSQL("update person set name=?,age=? where personid=?",person.getAge(),person.getId()});}public Cursor getCursor(int firstResult,int maxResult) {Cursor cursor = db.rawQuery("select personid as _id,name,age from person limit ?,?",new String[] {firstResult+"",maxResult+""} );return cursor;}public Person getPerson(Integer id) {Person person = null;Cursor cursor = db.rawQuery("select * from person where personid=?",new String[] {id+""} );while(cursor.moveToNext()) {person = new Person();person.setId(id);person.setName(cursor.getString(cursor.getColumnIndex("name")));person.setAge(cursor.getInt(cursor.getColumnIndex("age")));}return person;}public List<Person> findPersons(int firstResult,int maxResult) {List<Person> persons = new ArrayList<Person>();Person person = null;Cursor cursor = db.rawQuery("select personid as _id,maxResult+""} );while(cursor.moveToNext()) {person = new Person();person.setId(cursor.getInt(cursor.getColumnIndex("personid")));person.setName(cursor.getString(cursor.getColumnIndex("name")));person.setAge(cursor.getInt(cursor.getColumnIndex("age")));persons.add(person);}return persons;}public long getCount() {Cursor cursor = db.rawQuery("select count(*) from person",null);cursor.moveToFirst();return cursor.getLong(0);}} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |