QSqlQuery query;
query.prepare("DELETE FROM names WHERE id_col = :ID OR id_parent = :ID");
query.bindValue(":ID", idVal);
query.exec();
假設idVal將被綁定兩次,但id_parent = idVal執行這個查詢只能行被刪除,與id_col = idVal保持未刪除。所以只有第二次idVal綁定到查詢。
當我把它改寫到
QSqlQuery query;
query.prepare("DELETE FROM names WHERE id_col = ? OR id_parent = ?");
query.bindValue(0, idVal);
query.bindValue(1, idVal);
query.exec();
一切都按預期。
這是一種在QSqlQuery中多次使用一個指定佔位符的方法嗎?
**附註:**這不回答你的問題,但你可以'加入'刪除並只使用一個佔位符。例如:'DELETE n FROM names n INNER JOIN names p ON p.id_col = n.id_parent WHERE id_col =:ID'。 –
對於後代,當你在做選擇陳述時,我發現了一個相關問題的黑客。使用['with with clause'](https://www.sqlite.org/lang_with.html)如下所示:'with my_id as(values(:id))select * from names where id =(select * from my_id)或parent_id =(select * from my_id)' –
*吝嗇地*:在qt 5中刪除了這個約束,所以在qt 5之前只需要解決方法。 –