我们的Android开发人员知道, SQLiteDatabase execSQL
方法只能执行一个语句。
该医生说:
执行一个SQL语句不是一个查询。例如, CREATE TABLE
,删除
,插入
等。按分隔多个语句。
不能支持S
我有一个批次的记录,1000和计数加载。 我要如何将这些有效? 什么是提供这些sql语句与您的apk最简单的方法是什么?
我提到,已经有一个系统数据库,我会在的onupdate
赛事运行此。
我有这个code到目前为止:
名单,LT;的String []>李=新的ArrayList<的String []>();
li.add(新的String [] {
-1,停止,0
});
li.add(新的String [] {
0,开始,0
});
/ *在指定的其余部分* /
尝试 {
对于(字符串[] ELEM:利){
getDb()。execSQL(
INSERT INTO+ TABLENAME +(+ _ID +,+姓名+,+ PARSE_ORDER
+,ELEM))VALUES(,)???;
}
}赶上(例外五){
e.printStackTrace();
}
解决方案
我如何将这些有效?
使用事务:
db.beginTransaction();
尝试 {
对于(;;) {
db.execSQL(...);
}
db.setTransactionSuccessful();
} 最后 {
db.endTransaction();
}
As we Android developers know, the SQLiteDatabase execSQL
method can execute only one statement.
The doc says:
Execute a single SQL statement that is not a query. For example, CREATE TABLE
, DELETE
, INSERT
, etc. Multiple statements separated by ;
s are not supported.
I have to load in a batch of records, 1000 and counting. How do I insert these efficiently? And what's the easiest way to deliver these SQLs with your apk?
I mention, there is already a system database and I will run this on the onUpdate
event.
I have this code so far:
List<String[]> li = new ArrayList<String[]>();
li.add(new String[] {
"-1", "Stop", "0"
});
li.add(new String[] {
"0", "Start", "0"
});
/* the rest of the assign */
try {
for (String[] elem : li) {
getDb().execSQL(
"INSERT INTO " + TABLENAME + " (" + _ID + "," + NAME + "," + PARSE_ORDER
+ ") VALUES (?,?,?)", elem);
}
} catch (Exception e) {
e.printStackTrace();
}
解决方案
How do I insert these efficiently?
Use transaction:
db.beginTransaction();
try {
for(;;) {
db.execSQL(...);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
相关推荐
最新文章