android SQLite数据库的增删改查以及事务的单元测试
2010-11-05 00:56:30 来源:WEB开发网try {
db.setTransactionSuccessful();
db.execSQL("insert into person(name,age) values('Tom',21)");
} catch (Exception e) {
Log.i("aa", "trttttttttttttttttttttttttttttttttttttttttttttt");
}
db.endTransaction();// 事务结束;
}
public void save2(Person person) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.beginTransaction();// 开始事务
Log.i("aa", "uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu");
try {
db.setTransactionSuccessful();
for(int i=0;i< 100;i++){
db.execSQL("insert into person(name,age) values(?,?)",
new Object[] { person.getName(), person.getAge() });
}
} catch (Exception e) {
Log.i("aa", "trttttttttttttttttttttttttttttttttttttttttttttt");
}
db.endTransaction();// 事务结束;
}
public void update(Person person) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.execSQL(
"update person set name=?,age=? where personid=?",
new Object[] { person.getName(), person.getAge(),
person.getId() });
}
public Person find(Integer id) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = db.rawQuery(
"select personid,name,age from person where personid=?",
new String[] { String.valueOf(id) });
if (cursor.moveToNext()) {
Person person = new Person();
person.setId(cursor.getInt(cursor.getColumnIndex("personid")));
person.setName(cursor.getString(1));
person.setAge(cursor.getInt(2));
return person;
}
cursor.close();
return null;
}
public void delete(Integer id) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
更多精彩
赞助商链接