sqlite 开启事务可以提高效率

前端之家收集整理的这篇文章主要介绍了sqlite 开启事务可以提高效率前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近做一个东西,用到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>

猜你在找的Sqlite相关文章