加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite 开启事务可以提高效率

发布时间:2020-12-12 19:47:38 所属栏目:百科 来源:网络整理
导读:最近做一个东西,用到sqlite 插入100多条数据 大概要1700ms 大概1.7秒。我后来开起来事务。直接才161ms 效率提高十倍 span style="font-size:18px;"public void insertPO(ListPoetry poetryList) {SQLiteDatabase database = null;try {database = helper.get

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读