QSqlQuery - I couldn't export all the data from my SQL Database using one Data.
-
Well, I got Employee Entity. So, my idea is to export data from my SQL Database by clicking on the employee from the QTableView. This is the Employee Class:
class Employe{ private: int CIN; QString Nom; QString Prenom; QString email; QString Sexe; QString adresse; QString role; public: Employe(); Employe(int, QString, QString, QString, QString, QString, QString); ~Employe() {}; //Bunch of Setters and Getters here. bool ajouter(); QSqlQueryModel * afficher(); bool supprimer(int); };So I could get the CIN by clicking on the Employee but I want to extract the other Data (Nom, Prenom etc..) from the Employee Table using that Data CIN that I got. I am going to show you what came into my mind but I don't think it is working.
void MainWindow::on_UpdatepushButton_clicked() // Modification { ui->modify->show(); QModelIndex index = ui->tab_employe->currentIndex(); QString cin = index.data(Qt::DisplayRole).toString(); // Getting the CIN Data QSqlQuery query; query.prepare("SELECT nom, prenom, role FROM employe where cin=:cin"); ui->CIN_4->setText(cin); ui->CIN_5->setText(":nom"); ui->CIN_3->setText(":prenom"); ui->CIN_2->setText("hafedh.jendoubi@esprit.tn"); ui->CIN_6->setText("Manouba"); }See? So I am not familiar with SQL. Is there a way to extract the other data using the CIN data?
Thank you! -
Well, I got Employee Entity. So, my idea is to export data from my SQL Database by clicking on the employee from the QTableView. This is the Employee Class:
class Employe{ private: int CIN; QString Nom; QString Prenom; QString email; QString Sexe; QString adresse; QString role; public: Employe(); Employe(int, QString, QString, QString, QString, QString, QString); ~Employe() {}; //Bunch of Setters and Getters here. bool ajouter(); QSqlQueryModel * afficher(); bool supprimer(int); };So I could get the CIN by clicking on the Employee but I want to extract the other Data (Nom, Prenom etc..) from the Employee Table using that Data CIN that I got. I am going to show you what came into my mind but I don't think it is working.
void MainWindow::on_UpdatepushButton_clicked() // Modification { ui->modify->show(); QModelIndex index = ui->tab_employe->currentIndex(); QString cin = index.data(Qt::DisplayRole).toString(); // Getting the CIN Data QSqlQuery query; query.prepare("SELECT nom, prenom, role FROM employe where cin=:cin"); ui->CIN_4->setText(cin); ui->CIN_5->setText(":nom"); ui->CIN_3->setText(":prenom"); ui->CIN_2->setText("hafedh.jendoubi@esprit.tn"); ui->CIN_6->setText("Manouba"); }See? So I am not familiar with SQL. Is there a way to extract the other data using the CIN data?
Thank you!@Jendoubi-Hafedh
Hello and welcome.I'm afraid I don't get what your issue or thought is. If you have already filled the model with the rows in the table in order to display in a
QTableView, when you click on such an Employee row you look in the model at that row, you will see not only theCINcolumn but also all the other column values in that row, which is what you say you want. Why do you want to go back and issue a SQL query to the database for the row with theCINvalue when you already have that row read in? -
@Jendoubi-Hafedh
Hello and welcome.I'm afraid I don't get what your issue or thought is. If you have already filled the model with the rows in the table in order to display in a
QTableView, when you click on such an Employee row you look in the model at that row, you will see not only theCINcolumn but also all the other column values in that row, which is what you say you want. Why do you want to go back and issue a SQL query to the database for the row with theCINvalue when you already have that row read in?@JonB
Hello and thanks for your answer.I think you got the idea right. This is actually the picture of my desktop application:

As you can see, on the QTableView I only got CIN, Nom, Prenom & Role. My idea is, when I click on the employee and then click on "Modifier" button I get all the Employee information written on the "Ajouter Emploé(e)" Section. So if I rely on the Data placed in that QTableView, I will only get the CIN, Nom, Prenom & Role but instead, I want all the information. That's my main idea.
By the way, if you have any idea on how to show the CIN properly tell me please! Haha I wanted it be written simply, not like that "Scientific Writing" or whataever. Thanks!
-
@JonB
Hello and thanks for your answer.I think you got the idea right. This is actually the picture of my desktop application:

As you can see, on the QTableView I only got CIN, Nom, Prenom & Role. My idea is, when I click on the employee and then click on "Modifier" button I get all the Employee information written on the "Ajouter Emploé(e)" Section. So if I rely on the Data placed in that QTableView, I will only get the CIN, Nom, Prenom & Role but instead, I want all the information. That's my main idea.
By the way, if you have any idea on how to show the CIN properly tell me please! Haha I wanted it be written simply, not like that "Scientific Writing" or whataever. Thanks!
@Jendoubi-Hafedh said in QSqlQuery - I couldn't export all the data from my SQL Database using one Data.:
So if I rely on the Data placed in that QTableView, I will only get the CIN, Nom, Prenom & Role but instead, I want all the information. That's my main idea.
So your
QTableViewonly shows a few columns from the table? The simplest is probably to let the model fill with all the columns from the database table, you can hide columns in the table view. Otherwise you will presumably have to issue a new query against the data table for the selected primary key.For the "scientific notation" you need to look at something like QString QString::number(double n, char format = 'g', int precision = 6) (which is probably what it is using), the format option. Your numbers are so large that
gformat chooses to usee+...notation. You may need to add aQStyledItemDelegateto get the output desired in aQTableView, not sure.For the "detail view" (Ajouter Employe(e)) you might also like to look at Qt's QDataWidgetMapper Class.
-
@Jendoubi-Hafedh said in QSqlQuery - I couldn't export all the data from my SQL Database using one Data.:
So if I rely on the Data placed in that QTableView, I will only get the CIN, Nom, Prenom & Role but instead, I want all the information. That's my main idea.
So your
QTableViewonly shows a few columns from the table? The simplest is probably to let the model fill with all the columns from the database table, you can hide columns in the table view. Otherwise you will presumably have to issue a new query against the data table for the selected primary key.For the "scientific notation" you need to look at something like QString QString::number(double n, char format = 'g', int precision = 6) (which is probably what it is using), the format option. Your numbers are so large that
gformat chooses to usee+...notation. You may need to add aQStyledItemDelegateto get the output desired in aQTableView, not sure.For the "detail view" (Ajouter Employe(e)) you might also like to look at Qt's QDataWidgetMapper Class.
@JonB
Alright, thank you so much mate.