Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Wont show another window



  •    if(db.open()) {
            QMessageBox::information(this,"QMYSQL", "Sucesfull");
            QString username = ui->lineEdit_username->text();
            QString password = ui->lineEdit_password->text();
    
    
    
            QSqlQuery query(QSqlDatabase::database("QTUSERS"));
            query.prepare(QString("SELECT * FROM Users_QT WHERE username = :username AND password"));
    
    
            query.bindValue(":username", username);
            query.bindValue(":username", password);
    
            if(!query.exec()){
                QMessageBox::information(this, "Failed", "Nepodarilo sa pripojit na query");
            } else {
                while(query.next()){
                    QString usernameFromDB = query.value(1).toString();
                    QString passwordFromDB = query.value(2).toString();
    
                    qDebug()<<(usernameFromDB == username)<<(passwordFromDB == password);
    
                    if (usernameFromDB == username && passwordFromDB == password)  {
                        QMessageBox::information(this, "Uspesne", "Pripojil si query");
                        qDebug()<<"ops!";
    
    
    
                        Dialog logiin;
                        logiin.setModal(true);
                        logiin.show();
                        logiin.exec();
    
    
                    } else {
                        QMessageBox::information (this, "Failed", "Nepodarilo sa pripojit!");
                    }
    
                    }
                }
            }   else {
            QMessageBox::information(this,"QMYSQL", "Database not found");
        }
    
    
    
        }
    

    if login my app nothing happend only "Succesfull" succes is connect db correct but i cant connect to username and password i think i mean this :

               while(query.next()){
                    QString usernameFromDB = query.value(1).toString();
                    QString passwordFromDB = query.value(2).toString();
    
                    qDebug()<<(usernameFromDB == username)<<(passwordFromDB == password);
    
                    if (usernameFromDB == username && passwordFromDB == password)  {
                        QMessageBox::information(this, "Uspesne", "Pripojil si query");
                        qDebug()<<"ops!";
    

    this is my database :

    Screenshot 2020-09-20 at 21.52.53.png


  • Qt Champions 2019

    So what do you get exactly? QSqlQuery::prepare() and bindValue() have a return value which should be checked. Also QSqlQuery:lastError() should be used.



  • @Eternus said in Wont show another window:

    query.prepare(QString("SELECT * FROM Users_QT WHERE username = :username AND password"));

    Bad query. To the best of my knowledge, unless MySQL is strangely C-/Python-like.


  • Lifetime Qt Champion

    Hi,

    You are missing a : before password and you are binding twice to username.



  • @SGaist said in Wont show another window:

    You are missing a : before password

    It is missing the whole = :password after password, I assume.



  • @JonB @SGaist
    if i put this :

            query.prepare(QString("SELECT * FROM Users_QT WHERE username = :username AND password :password"));
    
    

    its says me this : if(!query.exec()){
    QMessageBox::information(this, "Failed", "Nepodarilo sa pripojit na query");

    but i dont know why mysql server running...



  • @Eternus said in Wont show another window:

    AND password :password

    Your query is still wrong. If you do not know what to put here you are going to struggle with MySQL.



  • @JonB query.prepare(QString("SELECT * FROM Users_QT WHERE username = :username AND password = :password"));

    if i put this its like before nothing show me ... not even that : QMessageBox::information(this, "Failed", "Nepodarilo sa pripojit na query");



  • @Eternus said in Wont show another window:

    not even that : QMessageBox::information(this, "Failed", "Nepodarilo sa pripojit na query");

    That's because for the first time you actually have a legitimate query.



  • @JonB what you mean ?
    i just following the tutorial.. and in turorial its works...



  • @Eternus
    You couldn't have followed whatever tutorial, because first you wrote

    AND password
    

    then you wrote

    AND password :password
    

    and now you write

    AND password = :password
    

    So how could the tutorial you are following have all 3 of these, when only the last one is a correct query? And you had at least your query.bindValue()s wrong, I don't know what else. Maybe you have not been correctly copying from the tutorial?


  • Lifetime Qt Champion

    Beside the good points of @JonB, does your database actually contain data ?



  • @Eternus
    Your code now does not error because it is syntactically correct. If it does not enter your output loop it is because there are simply no matching records for your SELECT statement. Either no rows have the username/password you are supplying, or as @SGaist says you simply have not put any rows into the table.



  • @JonB Solved thanks guyz



  • @Eternus said in Wont show another window:

    Solved thanks guyz

    Would you mind sharing what the solution was? For the benefit of other users of the forum...



  • @Pablo-J-Rogina
    ```
    QSqlQuery query(QSqlDatabase::database("QTUSERS"));

    
    to 
    

    QSqlQuery query(QSqlDatabase::database("MyConnect"));

    and 
            ```
    query.prepare(QString("SELECT * FROM Users_QT WHERE username = :username AND password"));
    

    to
    ```
    query.prepare(QString("SELECT * FROM Users_QT WHERE username = :username AND password = : password"));


Log in to reply