android 中 SQLite 数据库中用 insert 同时插入多条记录的方法和效率比较
在 Android 开发中我们常会遇到这样一种情况:在建立一个 SQLite 数据库时,
想同时插入大量数据。那么应该怎么做呢?
下面笔者以插入 20 条记录为例:
1. 将同时插入大量的数据写成一条 SQL 语句
2. 最笨的方法用 insert 语句一条一条的输入
3. 使用事务
代码如下:
public class DateBaseOpenHelper extends SQLiteOpenHelper {
public static nal String DBNAME="radiomap";
public static nal int VERSION=1;
public DateBaseOpenHelper(Context context){
super(context,DBNAME,null,VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table radiomap(location varchar(20),ap1 int,ap2
int)");
long a=System.currentTimeMillis();
db.execSQL("insert into radiomap(location,ap1,ap2) select
'x=1,y=1',-80,-73 " +
"union all select 'x=2,y=3',80,40 union all select 'x=3,y=5',30,20 "+
"union all select 'x=4,y=5',3,2 union all select 'x=30,y=50',30,20
union all select 'x=3,y=5',40,20"
+" union all select 'x=3,y=5',6,20 union all select 'x=3,y=5',6,7
union all select 'x=3,y=5',7,8 union all select 'x=3,y=5',8,9 union all
select 'x=3,y=5',9,9" +
" union all select 'x=3,y=5',3,5 union all select 'x=3,y=5',7,20 union
all select 'x=3,y=5',4,20 union all select 'x=3,y=5',5,20 union all