微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Qt 上使用 INSERT Query 修复 synthax 错误?

如何解决如何在 Qt 上使用 INSERT Query 修复 synthax 错误?

我正在尝试制作一个应用程序,用户可以在其中访问数据库并将行添加到同一数据库中。 问题是我有一个 sql 查询 synthax 错误,尽管我相信我写对了:

void FenPrincipale::boutonAjouter(){

requete.bindValue(":Nom",ajout_nom->text());
requete.bindValue(":Catégorie",liste_categories->currentText());
requete.bindValue(":Commentaires",ajout_description->toPlainText());;

qDebug() << liste_categories->currentText();
qDebug() << ajout_description->toPlainText();
qDebug() << ajout_nom->text();

requete.exec("INSERT INTO objets (Nom,Catégorie,Commentaires,Image)"
                " VALUES (:Nom,:Catégorie,:Commentaires,test )");

if(requete.exec()) {
   qDebug() << "Ok - requete";
    // Boucle qui permet de parcourir les enregistrements renvoyés par la requête
  while(requete.next()) {


            }//fin du while
}//fin du if

      else {
           qDebug() << "Echec de la requête : " << requete.lastError();
           }

}

我有这个错误

QsqlError("1064","QMysqL: Unable to execute query","您的 sql 语法有错误;请检查与您的 MariaDB 服务器版本相对应的手册,以在 ':Nom,附近使用正确的语法: Catégorie,test )' 在第 1 行")

我在网上到处搜索,但我找不到解决方案,我没有看到我在哪里做合成器错误...

你能帮我解决这个问题吗? 谢谢!

解决方法

您需要准备查询,然后向其中添加值

如果您的重音似乎有问题,请将其放在反引号中,或将其完全删除。

manual

requete.prepare("INSERT INTO objets (Nom,`Catégorie`,Commentaires,Image)"
                " VALUES (:Nom,:Catégorie,:Commentaires,test )");
requete.bindValue(":Nom",ajout_nom->text());
requete.bindValue(":Catégorie",liste_categories->currentText());
requete.bindValue(":Commentaires",ajout_description->toPlainText());
requete.exec

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。