public void inertOrUpdateDateBatch(List<String> sqls) {SQLiteDatabase db = getWritableDatabase();db.beginTransaction();try {for (String sql : sqls) {db.execSQL(sql);}// 设置事务标志为成功,当结束事务时就会提交事务db.setTransactionSuccessful();} catch (Exception e) {e.PRintStackTrace();} finally {// 结束事务db.endTransaction();db.close();}} |
db.beginTransaction(); // 手动设置开始事务for (ContentValues v : list) {db.insert("bus_line_station", null, v);}db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交db.endTransaction(); // 处理完成db.close() |
InsertHelper ih = new InsertHelper(db, "bus_line_station");db.beginTransaction();final int directColumnIndex = ih.getColumnIndex("direct");final int lineNameColumnIndex = ih.getColumnIndex("line_name");final int snoColumnIndex = ih.getColumnIndex("sno");final int stationNameColumnIndex = ih.getColumnIndex("station_name");try {for (Station s : busLines) {ih.prepareForInsert();ih.bind(directColumnIndex, s.direct);ih.bind(lineNameColumnIndex, s.lineName);ih.bind(snoColumnIndex, s.sno);ih.bind(stationNameColumnIndex, s.stationName);ih.execute();}db.setTransactionSuccessful();} finally {ih.close();db.endTransaction();db.close();} |
String sql = "insert into bus_line_station(direct,line_name,sno,station_name) values(?,?,?,?)";SQLiteStatement stat = db.compileStatement(sql);db.beginTransaction();for (Station line : busLines) {stat.bindLong(1, line.direct);stat.bindString(2, line.lineName);stat.bindLong(3, line.sno);stat.bindString(4, line.stationName);stat.executeInsert();}db.setTransactionSuccessful();db.endTransaction();db.close(); |
原文地址:http://www.blogjava.net/qileilove/archive/2014/05/23/414005.html
新闻热点
疑难解答