Connect MYSQL Database to QT app
-
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.
-
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 -
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 -
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 )
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)
-
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.
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.