菜鸟教程小白 发表于 2022-12-11 17:01:36

ios - 如何在 swift 中使用 dbAccess 的特定字段更新多条记录


                                            <p><p>我想更新多条记录 (DBAccess ORM)。根据特定字段的条件。比如 set city = "Goa"where name = "atul"。</p>

<p>请查看以下 swift 代码,它工作正常。但是如何在不使用 for 循环的情况下通过单个查询来做到这一点。 </p>

<pre><code>func updateRecordsByName(userName: String) {

    //like userName = atul;
    let userArr : DBResultSet = User.query().whereWithFormat(&#34;name = %@&#34;, withParameters:).fetch();

    for data in userArr {

      (data as! User).city = &#34;Goa&#34;;

      (data as! User).commit();
    }
}
</code></pre>

<p>请提出完美的解决方案以减少行数/循环数并改进上述查询代码。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>也没有办法,除了执行原始 SQL 本身就很危险,尤其是缓存和打开的对象不知道 SQLite 中已更新的值。 </p>

<p>循环对象有问题吗?如果它是性能,那么将更新包装在一个事务中,这样会加快它的速度。</p>

<pre><code>    DBTransaction.transaction({
         // put all your writes here

         for data in userArr {
             (data as! User).city = &#34;Goa&#34;;
             (data as! User).commit();
         }
    }) {
         // rollback
    }
</code></pre>

<p>也升级到 SharkORM,因为它现在已经从 DBAccess 接管:</p>

<p> <a href="http://sharkorm.com" rel="noreferrer noopener nofollow">http://sharkorm.com</a> , <a href="https://github.com/sharksync/sharkorm" rel="noreferrer noopener nofollow">https://github.com/sharksync/sharkorm</a> </p>

<p>如果需要,您可以随时向查询模块添加更新功能。或者通过问题提出功能请求。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 swift 中使用 dbAccess 的特定字段更新多条记录,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38482326/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38482326/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 swift 中使用 dbAccess 的特定字段更新多条记录