Delete where in clause in sqlite

老子叫甜甜 提交于 2021-01-28 14:19:38

问题


I have the following queries in sqlite which I am using in Xamarin Forms.

allProdsId is initialized as:

allProdsId = new List<Products>();

First of all, I made a query which retrieves all products ID from the table products. Then I want to delete all orders where orders have ProdId all the allProdsId retrieved.

The delete query is not working. Can someone please help me to achieve the WHERE IN clause in sqlite using xamarin forms ?

allProdsId = _con.Query<Products>("Select ProdId from Products");

con.Execute("DELETE FROM Orders WHERE ProdId = ?", allProdsId);

回答1:


Make a comma separated string of all the ProdIds that you wish to delete and insert that directly into the SQL string.

var productIDs = conn.Query<Products>("Select ProdId from Products");
var prodIDCommaString = string.Join(",", productIDs.Select(p => p.ProdId));
var deleteCount = conn.Execute("delete from Products where ProdId in (" + prodIDCommaString + ");");


来源:https://stackoverflow.com/questions/46795270/delete-where-in-clause-in-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!