本文介绍了多个插入语句android sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从网络下载了 800 个 insert
语句,格式如下:
I have 800 insert
statements downloaded from network in the following format:
insert into mytable(col1,..,coln) values(val1,..valn);insert into mytable col...
我听说过交易、ContentValue
s 和使用 union
,但我不知道哪种更适合这种情况.
I have heard of transactions ,ContentValue
s , and using union
but I can't figure out which one is better for this case.
截断mytable
后将它们插入sqlite数据库的最佳方法是什么?
What is the best way to insert them in sqlite database after truncating mytable
?
推荐答案
如果要运行多个查询,为了提高数据完整性和性能,应该使用事务.这是一段代码,可让您了解如何操作:
In case of multiple queries to run, in order to improve data integrity and performance, you should use transactions. This is a code to give you an idea how to do it:
SQLiteDatabase db = sqlHelper.getWritableDatabase(); // get a writable database here
db.beginTransaction();
try {
for (int insertQuery : listOfQueries) { // loop through your records
db.insert(...);
}
db.setTransactionSuccessful();
}
finally {
db.endTransaction();
}
db.close();
这篇关于多个插入语句android sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!