转载请注明出处:http://www.cnblogs.com/dachen408/p/7457312.html
程序设计过程中,经常要涉及到查询,并且有很多条件,且条件可为空,如果逐个判断,会有很多情况,解决方案:
QSqlQuery query(m_db);
QString province = QString::fromLocal8Bit("广东");
QString city = "";
QString null = "";
QString sql = QString("select * from tbSiteInfo where Province = '%1' and (City = '%2' or '%3' = '%4'“)).arg(province).arg(city).arg(city).arg(null);
query.exec(sql);;
增加了or判断语句,当city为空时也可以查询;