事务的使用

// 保存同步过来的User数据
public static void setUserList_YC(Context con, List<User> ulist) {

long beginTime = System.currentTimeMillis();
DatabaseHelper dbhelper = new DatabaseHelper(con, con.getString(R.string.dbname));
SQLiteDatabase db = dbhelper.getWritableDatabase();
// 开启事务 lilin 2012-1-16
db.beginTransaction();
for (User u : ulist) {
	String szm = "";
	szm = HanzhisToPinYin.cn2FirstSpell(u.name);
         db.execSQL(
          "insert into user(userguid,ouguid,name,phonenum,namepyshou) values(?,?,?,?,?)",
	new String[] { u.userguid, u.ouguid, u.name,u.phonenum, szm });
	}
	db.setTransactionSuccessful();// 设置事务标志为成功,当结束事务时就会提交事务
	db.endTransaction();// 结束事务
	long time = (System.currentTimeMillis() - beginTime) / 1000;
	System.out.println("保存人员时间:" + time);
	close(dbhelper, db, null);
}
 

你可能感兴趣的:(事务)