android SQLite数据库的增删改查以及事务的单元测试
2010-11-05 00:56:30 来源:WEB开发网}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVewsion) {
Log.i("aa","22222222222222");
db.execSQL("DROP TABLE IF EXISTS PERSON");
onCreate(db);
}
}
新建一个service服务类:
PersonService.java
package com.gohighsoft.db;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class PersonService {
private DataBaseHaler databaseHelper;
private Context context;
public PersonService(Context context) {
this.context = context;
databaseHelper = new DataBaseHaler(context);
}
public void savesTrannces(Person person) {
Log.i("aa", "4444444444444");
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.beginTransaction();// 开始事务
try {
for(int i=0;i< 100;i++){
db.execSQL("insert into person(name,age) values(?,?)",
new Object[] { person.getName(), person.getAge() });
}
db.execSQL("insert into personww(namedd,age) values(?,?)",
new Object[] { person.getName(), person.getAge() });
db.setTransactionSuccessful();
} catch (Exception e) {
//db.endTransaction();// 事务结束;
Log.i("aa","wwwwwwwwwwwwwwwwwwwwwwwww");
}
db.endTransaction();// 事务结束;
Log.i("aa", "55555555555555555555");
}
public void save1(Person person) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.beginTransaction();// 开始事务
Log.i("aa", "uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu");
更多精彩
赞助商链接