1. 在使用数据库时必须创建一个操作类继承SQLiteOpenHelper
package cn.service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class dbhelper extends SQLiteOpenHelper {
public dbhelper(Context context) {
super(context, "数据库名", null, num); //int num 版本号,当版本号发生变化时数据库会自动调用onUpgrade()方法
// TODO Auto-generated constructor stub
}
//数据库第一次创建时调用onCreate()
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(personid integer primary key autoincrement,name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("alter table person add phone varchar(12)");
}
}
2.根据实际情况为每个表创建类对象
package cn.domain;
public class person {
public Integer personid;
public String name;
public String phone;
public person(String name, String phone) {
this.name = name;
this.phone = phone;
}
public person(Integer personid, String name, String phone) {
this.personid = personid;
this.name = name;
this.phone = phone;
}
public Integer getPersonid() {
return personid;
}
public void setPersonid(Integer personid) {
this.personid = personid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "person [personid=" + personid + ", name=" + name + ", phone="
+ phone + "]";
}
}
3.为每个表创建数据库操作方法
package cn.domain;
import java.util.ArrayList;
import java.util.List;
import cn.service.dbhelper;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class personservice {
public dbhelper dber;
public personservice(Context context) {
this.dber = new dbhelper(context);
}
public void save(person p) {
SQLiteDatabase db = dber.getWritableDatabase();
db.execSQL("insert into person(name,phone) values(?,?)", new Object[] {
p.getName(), p.getPhone() });
db.close();
}
public void delete(Integer id) {
SQLiteDatabase db = dber.getWritableDatabase();
db.execSQL("delete from person where personid=?", new Object[] { id });
db.close();
}
public void update(person p) {
SQLiteDatabase db = dber.getWritableDatabase();
db.execSQL("update person set name=?,phone=? where personid=?",
new Object[] { p.getName(), p.getPhone(), p.getPersonid() });
db.close();
}
public person find(Integer id) {
SQLiteDatabase db = dber.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from person where personid=?",
new String[] { id.toString() });
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);
}
return null;
}
public List<person> getscrolldata(int offset, int maxresult) {
List<person> persons = new ArrayList<person>();
SQLiteDatabase db = dber.getReadableDatabase();
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"));
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 = dber.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from person", null);
cursor.moveToFirst();
long result = cursor.getLong(0);
return result;
}
}