public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context) {
super(context, "dbname", null, 1); //最后一个数字是数据库版本号
}
/**
* 第一次创建数据库时被执行,通常在此时创建表
*/
@Override
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) NULL");
}
}
使用API执行增删改查,内部是使用execSQL()方法执行SQL语句实现的。
public class PersonDbManager {
private DBOpenHelper dbOpenHelper;
public PersonDbManager(Context context){
dbOpenHelper = new DBOpenHelper(context);
}
/**
* 添加记录
*/
public void save(Person person){
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.execSQL("insert into person(name,phone)values(?,?)",
new Object[]{person.getName(),person.getPhone()});
}
public void saveWithApi(Person person){
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",person.getName());
values.put("phone",person.getPhone());
db.insert("person", null, values);
}
/**
* 删除记录
*/
public void delete(Integer id){
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.execSQL("delete from person where personid=?",
new Object[]{id});
}
public void deleteWithApi(Integer id){
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.delete("person","personid=?",new String[]{id.toString()});
}
/**
* 修改记录
*/
public void update(Person person){
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.execSQL("update person set name=?,phone=? where personid=?",
new Object[]{person.getName(),person.getPhone(),person.getId()});
}
public void updateWithApi(Person person){
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",person.getName());
values.put("phone",person.getPhone());
db.update("person",values,"personid=?",new String[]{person.getId().toString()});
}
/**
* 查找记录
*/
public Person find(Integer id){
SQLiteDatabase db = dbOpenHelper.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);
}
cursor.close();
}
public Person findWithApi(Integer id){
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("person", new String[]{"personid", "name", "phone"},
"personid=?", new String[]{id.toString()}, null, null, null);
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);
}
cursor.close();
}
/**
* 分页获取记录
* @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.rawQuery("select * from person order by personid asc limit ?,?",
new String[]{String.valueOf(offset),String.valueOf(maxResult)});
while(cursor.moveToFirst()){
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 List<Person> getScrollDataWithApi(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);
while(cursor.moveToFirst()){
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 = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from person",null);
cursor.moveToFirst();
long result = cursor.getLong(0);
cursor.close();
return result;
}
public long getCountWithApi(){
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("person",new String[]{"count(*)"},
null,null,null,null,null);
cursor.moveToFirst();
long result = cursor.getLong(0);
cursor.close();
return result;
}
}
事务操作
public void testTransaction(){
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.beginTransaction();//开始事务
try{
db.execSQL("...");
db.setTransactionSuccessful();//提交当前事务
}finally{
db.endTransaction();//由事务的标志决定是提交事务还是回滚事务
}
db.close();
}