Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connect MYSQL Database to QT app
Qt 6.11 is out! See what's new in the release blog

Connect MYSQL Database to QT app

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Lazar1L Offline
    Lazar1L Offline
    Lazar1
    wrote on last edited by
    #1

    I have created an app that calculates the gp for my university and i am supposed to connect it with a mysql db and when a user calculates his gp the grades whould be stored in the db. But after trying to do it i dont see any light when i press the button calculate the gp is ok but the numbers do not store in th db. Here is my code

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtMath>
    #include <QtSql>
    #include <QtDebug>
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    
        db.setHostName("localhost");
    
        db.setDatabaseName("vathmos");
    
        db.setUserName("root");
    
        db.setPassword("");
    
        db.open();
    
    }
    
    
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_3_clicked()
    {
    float m,y,c,k;
    QString x;
    k=0;
    
    
    x=ui->lineEdit->text();
    c=x.toFloat();
    if (c==0)
    k=k;
    else
    {
    y=c*5;
    k=5;
    }
    
      QSqlQuery ("INSERT INTO 'vathmoi' ( 'V_1') VALUES ('c' )") ;
    
    x=ui->lineEdit_3->text();
    c=x.toFloat();
    if (c==0)
    k=k;
    else
    {
    y=y+c*6;
    k=k+6;
    }
    
    QSqlQuery  ("INSERT INTO 'vathmoi' ( 'V_2') VALUES ('c' )") ;
    
    x=ui->lineEdit_2->text();
    c=x.toFloat();
    if (c==0)
    k=k;
    else
    {
    y=y+c*5;
    k=k+5;    
    }
    
    
    // there are 38 more line edits to insert the grade from each lesson and it ends like that
    
    
    x=ui->lineEdit_46->text();
    c=x.toFloat();
    if (c==0)
    k=k;
    else
    {
    y=y+c*10;
    k=k+10;
    }
    
    QString Qs;
    m=y/k;
    
    float mz,me;
    
    Qs = QString::number( m, 'g', 10 );
    ui->lineEdit_44->setText(Qs);
    
    me=6.5;
    mz=((me*213)-y)/(213-k);
    
    if ((mz<me) || (mz>10))
        Qs="Ανέφυκτος Μέσος Όρος";
    else
    {
    Qs = QString::number( mz, 'g', 10 );
    }
    
    ui->lineEdit_7->setText(Qs);
    
    me=7.5;
    mz=((me*213)-y)/(213-k);
    
    if ((mz<me) || (mz>10))
        Qs="Ανέφυκτος Μέσος Όρος";
    else
    {
    Qs = QString::number( mz, 'g', 10 );
    }
    
    ui->lineEdit_6->setText(Qs);
    
    me=8.5;
    mz=((me*213)-y)/(213-k);
    
    if ((mz<me) || (mz>10))
        Qs="Ανέφυκτος Μέσος Όρος";
    else
    {
    Qs = QString::number( mz, 'g', 10 );
    }
    
    ui->lineEdit_8->setText(Qs);
    }
    
    
    

    DO you have any idea how to solve this i would apriciate it very much.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome

      this looks a bit odd
      QSqlQuery ("INSERT INTO 'vathmoi' ( 'V_2') VALUES ('c' )") ;
      also, i see no exec()

      Normally it would be like

      QSqlQuery query;
      query.prepare("INSERT INTO person (id, forename, surname) "
      "VALUES (?, ?, ?)");
      query.addBindValue(1001);
      query.addBindValue("Bart");
      query.addBindValue("Simpson");
      query.exec(); // this runs it

      Lazar1L 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi and welcome

        this looks a bit odd
        QSqlQuery ("INSERT INTO 'vathmoi' ( 'V_2') VALUES ('c' )") ;
        also, i see no exec()

        Normally it would be like

        QSqlQuery query;
        query.prepare("INSERT INTO person (id, forename, surname) "
        "VALUES (?, ?, ?)");
        query.addBindValue(1001);
        query.addBindValue("Bart");
        query.addBindValue("Simpson");
        query.exec(); // this runs it

        Lazar1L Offline
        Lazar1L Offline
        Lazar1
        wrote on last edited by
        #3

        Is this right?

        QSqlQuery query;
        query.prepare("INSERT INTO vathmoi (V_1) "
        "VALUES (c)");
        query.exec(); // this runs it
        

        I want to incert the c variable to the db (the c variable is the grade that i student had in a lesson )

        the_T 1 Reply Last reply
        0
        • Lazar1L Lazar1

          Is this right?

          QSqlQuery query;
          query.prepare("INSERT INTO vathmoi (V_1) "
          "VALUES (c)");
          query.exec(); // this runs it
          

          I want to incert the c variable to the db (the c variable is the grade that i student had in a lesson )

          the_T Offline
          the_T Offline
          the_
          wrote on last edited by
          #4

          @Lazar1

          There are two ways to prepare a sql statement.

          • with named parameters would be
          query.prepare("INSERT INTO person (id, forename, surname) "
          "VALUES (:id, :forename, :surename)");
          

          and then binding the values to the parameters with

          query.bindValue(":id", id);
          query.bindValue(":forename",forename);
          query.bindValue(":surename", surename);
          
          • the way as @mrjj described with unnamed parameters

          (I for myself prefer the named parameters for more insight with long query strings)

          -- No support in PM --

          1 Reply Last reply
          3
          • Lazar1L Offline
            Lazar1L Offline
            Lazar1
            wrote on last edited by
            #5

            Thank you for your answer.

            I used the one with the named parameters exactly as you mentioned but when i go and chech in php my admin in vathmoi table there is nothing written. and qt doesnt state any problem with the connection to the db.

            the_T 1 Reply Last reply
            0
            • Lazar1L Lazar1

              Thank you for your answer.

              I used the one with the named parameters exactly as you mentioned but when i go and chech in php my admin in vathmoi table there is nothing written. and qt doesnt state any problem with the connection to the db.

              the_T Offline
              the_T Offline
              the_
              wrote on last edited by
              #6

              @Lazar1

              Maybe some debug outputs will help

              if(!db.open()) {
              qDebug() << "DB Connect Error: " << db.lastError();
              }
              

              this will write the sql error when the database can not be opened.

              if(!query.exec()) {
              qDebug() << "SQL Statement Error" << query.lastError();
              }
              

              will print the error when the execution of the sql statement failed.

              -- No support in PM --

              1 Reply Last reply
              4
              • Lazar1L Offline
                Lazar1L Offline
                Lazar1
                wrote on last edited by
                #7

                @The_

                Thank you very much

                while debuging with the code you worte i realized that there is no problem with the conection but with the sql statement. I am colsing this toping as solved.

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved