[SOLVED]MYSQL query not working



  • Hello!

    I would like ask for help.
    In the gepadatok function not working none query, but in the jszamfeltolt is working.
    Why?

    header file:

    @#ifndef JADATBAZISA_H
    #define JADATBAZISA_H

    #include "adatbaziskapcsolat.h"
    #include "korok.h"
    #include "QtDebug"

    class Jadatbazisa : public AdatbazisKapcsolat
    {
    //AdatbazisKapcsolat *adatbazis;

    public:
    Jadatbazisa();
    ~Jadatbazisa();

    void jszamfeltolt(int szam);
    void gepadatok(QString gnev, QString gpakli);
    

    private:
    //QSqlQuery query;
    QString gepszam;
    };

    #endif // JADATBAZISA_H
    @

    .cpp file:

    @#include "jadatbazisa.h"

    Jadatbazisa::Jadatbazisa()
    {
    //adatbazis = new AdatbazisKapcsolat();
    }

    Jadatbazisa::~Jadatbazisa()
    {
    }

    void Jadatbazisa::jszamfeltolt(int szam)
    {
    adatbkapcsolat();
    QSqlQuery query(db);

    db.open();
    gepszam = QString::number(szam);
    query.exec("SELECT * FROM gepszam WHERE id = 1");
    
    if(!query.first())
    {
        query.exec("INSERT INTO gepszam(gepszam) VALUES("+gepszam+")");
    }else{
        //qDebug() << "jszamfeltölt update: " << query.isActive();
        query.exec&#40;"UPDATE gepszam SET gepszam = "+gepszam+";"&#41;;
    }
    
    db.close(&#41;;
    

    }

    //Paklik feltöltése az adatbázisba
    void Jadatbazisa::gepadatok(QString gnev, QString gpakli)
    {
    adatbkapcsolat();
    QSqlQuery query(db);

    db.open();
    
    if(query.exec&#40;"SELECT * FROM teszt WHERE gepnev = "+gnev+""&#41;&#41;
    {
        qDebug(&#41; << "gepadatok select true";
    }
    
    if(!query.first())
    {
        if(query.exec&#40;"INSERT INTO teszt(nev&#41; VALUES("+gnev+"&#41;"&#41;)
        {
            qDebug() << "gepadatok insert true";
        }
    }else{
        query.exec("UPDATE gepek SET pakli = "+gpakli+" WHERE gepnev = "+gnev+""&#41;;
    }
    
    db.close(&#41;;
    

    }@

    Thank you and sorry my English language!


  • Moderators

    Hi,

    welcome to Devnet,

    have a look at the "docs about database binding":http://qt-project.org/doc/qt-5/qsqlquery.html#approaches-to-binding-values



  • Hi, if gnev contains nonnumeric strings you should set it in single quotation marks:
    "SELECT * FROM teszt WHERE gepnev = '"+gnev+"'"

    EDIT:
    "SELECT * FROM teszt WHERE gepnev = '" + gnev + "'"



  • Thank you Eddy and Clochydd!

    It's working:

    @query.prepare("INSERT INTO teszt (gepnev, pakli) "
    "VALUES (:gnev, :gpakli)");
    query.bindValue(":gnev", gnev);
    query.bindValue(":gpakli", gpakli);
    query.exec()@


  • Moderators

    You're welcome,

    Please edit the title of your first post and prepend [solved] to it so that others can see easily it in the overview.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.