• Qt执行带参sql


    1. //准备执行的sql语句,此为带参的sql语句
    2. query.prepare("update employee set Name=:Name, Gender=:Gender,Height=:Height,"
    3. " Birthday=:Birthday, Mobile=:Mobile, Province=:Province,"
    4. " City=:City, Department=:Department, Education=:Education,"
    5. " Salary=:Salary, Memo=:Memo, Photo=:Photo "
    6. " where EmpNo = :ID");
    7. //设置sql语句中参数的值
    8. query.bindValue(":Name",recData.value("Name"));
    9. query.bindValue(":Gender",recData.value("Gender"));
    10. query.bindValue(":Height",recData.value("Height"));
    11. query.bindValue(":Birthday",recData.value("Birthday"));
    12. query.bindValue(":Mobile",recData.value("Mobile"));
    13. query.bindValue(":Province",recData.value("Province"));
    14. query.bindValue(":City",recData.value("City"));
    15. query.bindValue(":Department",recData.value("Department"));
    16. query.bindValue(":Education",recData.value("Education"));
    17. query.bindValue(":Salary",recData.value("Salary"));
    18. query.bindValue(":Memo",recData.value("Memo"));
    19. query.bindValue(":Photo",recData.value("Photo"));
    20. query.bindValue(":ID",empNo);
    21. //执行对应绑定值的sql语句
    22. if (!query.exec())
    23. QMessageBox::critical(this, "错误", "记录更新错误\n"+query.lastError().text(),
    24. QMessageBox::Ok,QMessageBox::NoButton);

    删除:

    1. QSqlQuery query;
    2. query.prepare("delete from employee where EmpNo = :ID");
    3. query.bindValue(":ID",empNo);
    4. if (!query.exec())
    5. QMessageBox::critical(this, "错误", "删除记录出现错误\n"+query.lastError().text(),
    6. QMessageBox::Ok,QMessageBox::NoButton);

    修改

    1. QSqlQuery qryUpdate; //临时 QSqlQuery
    2. qryUpdate.prepare("UPDATE employee SET Salary=:Salary WHERE EmpNo = :ID");
    3. qryUpdate.bindValue(":ID",empID);
    4. qryUpdate.bindValue(":Salary",salary); //设置SQL语句参数
    5. qryUpdate.exec(); //执行update

    插入:

    1. query.prepare("INSERT INTO employee (EmpNo,Name,Gender,Height,Birthday,Mobile,Province,"
    2. " City,Department,Education,Salary,Memo,Photo) "
    3. " VALUES(:EmpNo,:Name, :Gender,:Height,:Birthday,:Mobile,:Province,"
    4. " :City,:Department,:Education,:Salary,:Memo,:Photo)");
    5. query.bindValue(":EmpNo",recData.value("EmpNo"));
    6. query.bindValue(":Name",recData.value("Name"));
    7. query.bindValue(":Gender",recData.value("Gender"));
    8. query.bindValue(":Height",recData.value("Height"));
    9. query.bindValue(":Birthday",recData.value("Birthday"));
    10. query.bindValue(":Mobile",recData.value("Mobile"));
    11. query.bindValue(":Province",recData.value("Province"));
    12. query.bindValue(":City",recData.value("City"));
    13. query.bindValue(":Department",recData.value("Department"));
    14. query.bindValue(":Education",recData.value("Education"));
    15. query.bindValue(":Salary",recData.value("Salary"));
    16. query.bindValue(":Memo",recData.value("Memo"));
    17. query.bindValue(":Photo",recData.value("Photo"));
    18. if (!query.exec())
    19. QMessageBox::critical(this, "错误", "插入记录错误\n"+query.lastError().text(),
    20. QMessageBox::Ok,QMessageBox::NoButton);

  • 相关阅读:
    GEE图表——利用NOAA气象数据绘制气温预测图
    基于django的网络隐私检测系统
    低代码助力生产管理:健康安全环境管理系统
    科研方法-X_LAB-方法总结和实践记录
    修改cdh6.3.2集群内部弱口令步骤
    2. kafka消息队列
    3 数据链路层(二):差错控制、差错检测和纠正
    matlab 多目标粒子群优化算法MOPSO
    java毕业设计公司日常考勤系统mybatis+源码+调试部署+系统+数据库+lw
    全链路压测:影子库与影子表之争
  • 原文地址:https://blog.csdn.net/weixin_38241876/article/details/134385211