标签:android des style color 使用 os io strong
* @author Administrator yangchao
*为了实现对数据库版本进行管理,SQLiteOpenHelper类提供了两个重要的方法,
*分别是onCreate(SQLiteDatabase db)和onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
*/
public class DBOpenHelper extends SQLiteOpenHelper
{
private static final String DatabaseName = "test.db";//数据库的名称
private static final int DatabaseVersion = 1;//数据库的版本号
public DBOpenHelper(Context context)
{
super(context, DatabaseName, null, DatabaseVersion);
}
/*
* 用于初次使用软件时生成数据库表
*/
@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)
{
}
}
------------------------------------------------------------------------------------------------------------------------------------
public class PersonService
{
DBOpenHelper dbOpenHelper;
public PersonService(){}
public PersonService(Context context)
{
this.dbOpenHelper = new DBOpenHelper(context);
}
/**
* 保存方法
* @param person
*/
public void save(Person person)
{
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
String sql = "insert into person(personId, name) values(?, ?)";
database.execSQL(sql, new Object[]{person.getPersonId(), person.getName()});
}
/**
* 更改方法
* @param person
*/
public void update(Person person)
{
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
String sql = "update person set name=? where personId=?";
database.execSQL(sql, new Object[]{person.getName(), person.getPersonId()});
}
/**
* 删除方法
* @param person
*/
public void delete(Person person)
{
SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
String sql = "delete from person where personId=?";
database.execSQL(sql, new Object[]{person.getPersonId()});
}
/**
* 查找单个记录
* @param id
* @return
*/
public Person find(Integer id)
{
SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
String sql = "select * from person where personId = ?";
Cursor cursor = database.rawQuery(sql, new String[]{id.toString()});
if(cursor.moveToFirst())
{
String name = cursor.getString(cursor.getColumnIndex("name"));
int personId = cursor.getInt(cursor.getColumnIndex("personId"));
return new Person(personId, name);
}
return null;
}
/**
* 分页查找
* @param offset
* @param maxResult
* @return
*/
public List<Person> getScrollData(Integer offset, Integer maxResult)
{
List<Person> persons = new ArrayList<Person>();
SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
String sql = "select * from person limit ?, ?";
Cursor cursor = database.rawQuery(sql, new String[]{offset.toString(), maxResult.toString()});
while(cursor.moveToNext())
{
int personId = cursor.getInt(cursor.getColumnIndex("personId"));
String name = cursor.getString(cursor.getColumnIndex("name"));
persons.add(new Person(personId, name) );
}
return persons;
}
/**
* 获取总记录数
* @return
*/
public long getCount()
{
SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
String sql = "select count(*) from person";
Cursor cursor = database.rawQuery(sql, null);
cursor.moveToFirst();
return cursor.getLong(0);
}
}
====================================测试======================================
public class DBTest extends AndroidTestCase
{
private static final String TAG = "DBTest";
public void testCreateDB()
{
DBOpenHelper helper = new DBOpenHelper(this.getContext());
/*
* 当调用SQLiteOpenHelper的getWritableDatabase()或者getReadableDatabase()方法获取
* 用于操作数据库的SQLiteDatabase实例的时候,如果数据库不存在,Android系统会自动生成一个数据库,
* 接着调用onCreate()方法
*/
helper.getWritableDatabase();
}
public void testSava() throws Exception
{
PersonService service = new PersonService(this.getContext());
service.save(new Person(1, "杨超"));
service.save(new Person(2, "杨超1"));
service.save(new Person(3, "杨超2"));
}
public void testDelete()
{
}
public void testFind()
{
PersonService service = new PersonService(this.getContext());
Person person = service.find(4);
Log.i(TAG, person.toString());
}
public void testFenye()
{
}
public void testCount()
{
}
}
Android——使用SQLiteDatabase操作SQLite数据库,布布扣,bubuko.com
Android——使用SQLiteDatabase操作SQLite数据库
标签:android des style color 使用 os io strong
原文地址:http://blog.csdn.net/yangchao13341408947/article/details/38421599