Android SQLite插入优化
最初代码如下,直接执行sql语句,外加事务提升性能:
SQLiteDatabase database = new SQLiteDatabase(); if (database.isOpen()) { database.beginTransaction(); try { //sql为insert into tableName (name) values ("test") database.execSQL(sql); } database.setTransactionSuccessful(); } finally { database.endTransaction(); } database.close(); }
优化如下:
SQLiteDatabase database = new SQLiteDatabase(); //sql为insert into tableName (name) values (?) SQLiteStatement sqlListStatment = database.compileStatement(sql); if (database.isOpen()) { database.beginTransaction(); try { //index 为1开始索引,value为入库的值 //bingXXX为插入XXX类型 sqLiteStatement.bindString(index, value); sqLiteStatement.executeInsert(); } database.setTransactionSuccessful(); } finally { database.endTransaction(); } database.close(); }
相关推荐
ribavnu 2020-11-16
要啥自行车一把梭 2020-11-12
vitasfly 2020-11-12
ILVNMM 2020-09-19
zycchun 2020-10-16
康慧欣 2020-09-10
liuweiq 2020-09-09
silencehgt 2020-09-07
sunnyxuebuhui 2020-09-07
西瓜皮儿的皮儿 2020-09-07
李轮清 2020-09-15
lifeison 2020-08-18
cyhgogogo 2020-08-18
minerk 2020-08-15
webliyang 2020-08-15
houdaxiami 2020-08-15
抱抱熊 2020-08-15
caodayong 2020-08-15
徐悦TechBlog 2020-08-15