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

在SQLite数据库android中插入多个条目

发布时间:2020-12-12 18:52:50 所属栏目:百科 来源:网络整理
导读:我在向 Android SQLite数据库中插入多个条目时遇到问题.我这样插入db.execQuery(): String insertString = "INSERT INTO coordinates SELECT 0 AS _id,5 AS associated_route_id,38.88945 AS latidude,-77.034821 AS longitude UNION SELECT 1,5,38.889671,
我在向 Android SQLite数据库中插入多个条目时遇到问题.我这样插入db.execQuery():

String insertString = "INSERT INTO coordinates 
    SELECT 0 AS _id,5 AS associated_route_id,38.88945 AS latidude,-77.034821 AS longitude 
    UNION SELECT 1,5,38.889671,-77.034912 
    UNION SELECT 2,38.890041,-77.035316"
database.execSQL(insertString);

我稍后用同样的数据库拉,如下:

String[] columns = new String[] { "latitude","longitude" };
Cursor cursor = db.query("coordinates",columns,"associated_route_id=5",null,null);

cursor.moveToFirst();
if (cursor.getCount() == 0)
// No rows in cursor

我使用db.insert(table,contentValues)使用它,但替换了插入以使事情更快.

问题是光标是空的,这使得它看起来像插入不起作用.为什么不起作用?

解决方法

回答我自己的问题.

我使用了不同的方法来插入条目.我使用Trinimon的建议来使用db.compileStatement,但增加插入时间最多的是添加:

db.startTransaction();
//insert lots of stuff...
database.setTransactionSuccessful();
database.endTransaction();

插入500个条目从45秒减少到300毫秒.

(编辑:李大同)

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

    推荐文章
      热点阅读