// 添加player,如果有就更新,没有就添加。 public void add(BubblePlayer player) { db.beginTransaction(); // 开始事务 try { BubblePlayer personInDB = new BubblePlayer(); personInDB = query(player.getId()); // 如果没有找到就直接添加,如果找到了就更新 if (null == personInDB) { db.execSQL( "INSERT INTO starchat_bubbleplayer(id, name, latitude, lontitude, headImg, sex) VALUES(?, ?, ?, ?, ?, ?)", new Object[] { player.getId(), player.getName(), player.getLatitude(), player.getLontitude(), player.getHeadImg(), player.getSex() }); } else { updatePlayer(player); } db.setTransactionSuccessful(); // 设置事务成功完成 } finally { db.endTransaction(); // 结束事务 } }
/** add persons */ public void add(List<BubblePlayer> bubbleplayers) { db.beginTransaction(); // 开始事务 try { for (BubblePlayer player : bubbleplayers) { // db.execSQL("INSERT INTO starchat_bubbleplayer(id, name, latitude, lontitude ) VALUES(5, // 'name', 31, 130)"); BubblePlayer personInDB = new BubblePlayer(); personInDB = query(player.getId()); if (null == personInDB) { db.execSQL( "INSERT INTO starchat_bubbleplayer(id, name, latitude, lontitude, headImg, sex) VALUES(?, ?, ?, ?, ?, ?)", new Object[] { player.getId(), player.getName(), player.getLatitude(), player.getLontitude(), player.getHeadImg(), player.getSex() }); } else { updatePlayer(player); } } db.setTransactionSuccessful(); // 设置事务成功完成 } finally { db.endTransaction(); // 结束事务 } }
/** * update person's age * * @param person */ public void updatePlayer(BubblePlayer person) { ContentValues cv = new ContentValues(); cv.put("id", person.getId()); cv.put("name", person.getName()); cv.put("latitude", person.getLatitude()); cv.put("lontitude", person.getLontitude()); cv.put("headImg", person.getHeadImg()); cv.put("sex", person.getSex()); db.update("starchat_bubbleplayer", cv, "id = ?", new String[] {String.valueOf(person.getId())}); }