最近做一个东西,用到sqlite 插入100多条数据 大概要1700ms 大概1.7秒。我后来开起来事务。直接才161ms 效率提高十倍
<span style="font-size:18px;"> public void insertPO(List<Poetry> poetryList) { sqliteDatabase database = null; try { database = helper.getWritableDatabase(); <span style="background-color: rgb(255,0);">database.beginTransaction();</span> for (int i = 0; i < poetryList.size(); i++) { Poetry poetry = poetryList.get(i); ContentValues cv = new ContentValues(); cv.put("poetryid",poetry.getPoetryid()); cv.put("title",poetry.getTitle()); cv.put("kindid",poetry.getKindid()); cv.put("typeid",poetry.getTypeid()); cv.put("writerid",poetry.getWriterid()); cv.put("content",poetry.getContent()); if (!helper.isExist("Poetry","poetryid",poetry.getPoetryid(),database)) { database.insert("Poetry",null,cv); } } <span style="background-color: rgb(255,0);">database.setTransactionSuccessful();</span> } catch (Exception e) { e.printStackTrace(); } finally { if (database != null) { <span style="background-color: rgb(255,0);">database.endTransaction();</span> database.close(); } } }</span>