多次插入或Android的SQLite的替换查询 [英] Multiple Insert or Replace Query in Android SQLite

查看:415
本文介绍了多次插入或Android的SQLite的替换查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的查询插入或更换联系人表数量和时间。有没有什么办法可以插入或更换多个纪录的SQLite Android的?

 插入或更换成联系人(_id,数量,状态)值((从通讯录中选择_id其中编号='123456'),'123456','送');


解决方案

您可以提高速度多/批量数据库中插入或使用事务和compileStatement让您的查询将只编译一次观念更换操作。

例如:

  db.beginTransaction();
    尝试{
         SQL字符串=(?,?,?,)插入或更换成件物品(itemNumber,描述,unitOfMeasure,重量比)值;
         ArrayList的< ItemMaster> itemsList = //这里找回您的作品列表
         的for(int i = 0; I< itemsList.size();我++)
         {             SQLiteStatement插入= db.compileStatement(SQL);
             insert.bindString(1,item.getItemNumber());
             insert.bindString(2,item.getItemDescription1());
             insert.bindString(3,item.getSellingUOM());
             insert.bindDouble(4,item.getWeight());
             insert.execute();
         }
         db.setTransactionSuccessful();
    } {最后
      db.endTransaction();
    }

I am using below query to insert or replace "number" and "time" in "Contacts" table. Is there any way I can insert or replace multiple record in SQLite for Android?

"Insert or replace into Contacts (_id, number, status) values ((select _id from Contacts where number = '123456'), '123456','sent');"

解决方案

You can improve speed for Multiple/Batch Database Inserting or Replacing operation using concept of transaction and compileStatement so your query will be compiled only once.

For Example:

    db.beginTransaction();
    try {
         String sql = "Insert or Replace into Items (itemNumber, description,unitOfMeasure, weight) values(?,?,?,?)";
         ArrayList<ItemMaster> itemsList = // Retrieve your items list here
         for(int i=0;i<itemsList.size();i++)
         {

             SQLiteStatement insert = db.compileStatement(sql);
             insert.bindString(1, item.getItemNumber());
             insert.bindString(2, item.getItemDescription1());
             insert.bindString(3, item.getSellingUOM());
             insert.bindDouble(4, item.getWeight());
             insert.execute();
         }
         db.setTransactionSuccessful();
    } finally {
      db.endTransaction();
    }

这篇关于多次插入或Android的SQLite的替换查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆