package com.travelsky.service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
/**
*
* 作者:wtmax<br>
* 功能描述: SQLite操纵类,可以生成及更新指定名称数据库<br>
* 创建时间:2011-09-21 21:56:57<br>
* 版本信息:1.0 <br>
* Copyright: Copyright (c) 2011<br>
*/
public class DataBaseOpenHelper extends SQLiteOpenHelper {
public static final String DBNAME = "travelsky";
public static final int version = 1;
public DataBaseOpenHelper(Context context) {
super(context, DBNAME, null, version);//数据库版本如有发生变更,就会调用onUpgrade方法
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE person (personid integer primary key autoincrement, name varchar(20), age INTEGER)");
}
/**
* 数据库版本如有发生变更,就会调用onUpgrade方法
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS person");
onCreate(db);
}
}
package com.travelsky.service;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.travelsky.domain.Person;
/**
*
* 作者:wtmax<br>
* 功能描述: SQLite操纵类,可以对指定名称数据库中的表进行CRUD操作<br>
* 创建时间:2011-09-21 22:28:57<br>
* 版本信息:1.0 <br>
* Copyright: Copyright (c) 2011<br>
*/
public class PersonService {
private DataBaseOpenHelper dbOpenHelper;
public PersonService(Context context) {
dbOpenHelper = new DataBaseOpenHelper(context);
}
public void saves(){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
database.beginTransaction();
try{
database.execSQL("insert into person(name, age) values(?,?)", new Object[]{"立马", (short)32});
database.execSQL("insert into person(name, age) values(?,?)", new Object[]{"当下", (short)37});
database.setTransactionSuccessful();//提交事务(做到事务统一)
}catch(Exception e){
}
database.endTransaction();
// database.close();
}
public void save(Person person){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
database.execSQL("insert into person(name, age) values(?,?)", new Object[]{person.getName(), person.getAge()});
// database.close();
}
public void update(Person person){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
database.execSQL("update person set name=?, age=? where personid=?",
new Object[]{person.getName(), person.getAge(), person.getId()});
}
public Person find(Integer id){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
Cursor cursor = database.rawQuery("select * from person where personid=?", new String[]{String.valueOf(id)});
if(cursor.moveToNext()){
return new Person(cursor.getInt(0),cursor.getString(1),cursor.getShort(2));
}
return null;
}
public void delete(Integer... ids){
if(ids.length>0){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
StringBuffer sb = new StringBuffer();
for(Integer id : ids){
sb.append('?').append(",");
}
sb.deleteCharAt(ids.length-1);
database.execSQL("delete person where personid in("+sb+")",
new Object[]{ids});
}
}
public List<Person> getScrollData(int startResult, int maxResult){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
List<Person> persons = new ArrayList<Person>();
Cursor cursor = database.rawQuery("select * from person limit ?, ?",
new String[]{String.valueOf(startResult), String.valueOf(maxResult)});
while(cursor.moveToNext()){
persons.add(new Person(cursor.getInt(0),cursor.getString(1),cursor.getShort(2)));
}
return persons;
}
public long getCount(){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
Cursor cursor = database.rawQuery("select count(*) from person", null);
if(cursor.moveToNext()){
return cursor.getLong(0);
}
return 0;
}
}
package com.travelsky.service;
import java.util.ArrayList;
import java.util.List;
import com.travelsky.domain.Person;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class OtherPersonService {
private DataBaseOpenHelper dbOpenHelper;
public OtherPersonService(Context context) {
dbOpenHelper = new DataBaseOpenHelper(context);
}
public void save(Person person){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", person.getName());
values.put("age", person.getAge());
database.insert("person", "name", values);
}
public void update(Person person){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", person.getName());
values.put("age", person.getAge());
database.update("person", values, "personid=?", new String[]{String.valueOf(person.getId())});
}
public Person find(Integer id){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
Cursor cursor = database.query("person", new String[]{"personid", "name", "age"},
"personid=?", new String[]{String.valueOf(id)}, null, null, null);
if(cursor.moveToNext()){
return new Person(cursor.getInt(0), cursor.getString(1), cursor.getShort(2));
}
return null;
}
public void delete(Integer... ids){
if(null != ids && ids.length>0){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
StringBuffer sb = new StringBuffer();
String[] sbIds = new String[sb.length()];
for(int i=0;i<ids.length;i++){
sb.append('?').append(',');
sbIds[i] = String.valueOf(ids[i]);
}
sb.deleteCharAt(sb.length()-1);
database.delete("person", "personid in("+sb+")", sbIds);
}
}
public List<Person> getScrollData(int startResult, int maxResult){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
List<Person> persons = new ArrayList<Person>();
Cursor cursor = database.query("person", new String[]{"personid", "name", "age"},
null, null, null, null, "personid desc", startResult + "," + maxResult);
while(cursor.moveToNext()){
persons.add(new Person(cursor.getInt(0), cursor.getString(1), cursor.getShort(2)));
}
return persons;
}
public int count(){
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
Cursor cursor = database.query("person", new String[]{"count(*)"}, null, null, null, null, null);
if(cursor.moveToNext()){
return cursor.getInt(0);
}
return 0;
}
}