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. QSqlQuery - I couldn't export all the data from my SQL Database using one Data.
Qt 6.11 is out! See what's new in the release blog

QSqlQuery - I couldn't export all the data from my SQL Database using one Data.

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 423 Views
  • 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.
  • Jendoubi.HafedhJ Offline
    Jendoubi.HafedhJ Offline
    Jendoubi.Hafedh
    wrote on last edited by
    #1

    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!

    JonBJ 1 Reply Last reply
    0
    • Jendoubi.HafedhJ Jendoubi.Hafedh

      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!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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 the CIN column 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 the CIN value when you already have that row read in?

      Jendoubi.HafedhJ 1 Reply Last reply
      1
      • JonBJ JonB

        @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 the CIN column 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 the CIN value when you already have that row read in?

        Jendoubi.HafedhJ Offline
        Jendoubi.HafedhJ Offline
        Jendoubi.Hafedh
        wrote on last edited by Jendoubi.Hafedh
        #3

        @JonB
        Hello and thanks for your answer.

        I think you got the idea right. This is actually the picture of my desktop application:

        alt text

        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!

        JonBJ 1 Reply Last reply
        0
        • Jendoubi.HafedhJ Jendoubi.Hafedh

          @JonB
          Hello and thanks for your answer.

          I think you got the idea right. This is actually the picture of my desktop application:

          alt text

          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!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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 QTableView only 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 g format chooses to use e+... notation. You may need to add a QStyledItemDelegate to get the output desired in a QTableView, not sure.

          For the "detail view" (Ajouter Employe(e)) you might also like to look at Qt's QDataWidgetMapper Class.

          Jendoubi.HafedhJ 1 Reply Last reply
          3
          • JonBJ JonB

            @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 QTableView only 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 g format chooses to use e+... notation. You may need to add a QStyledItemDelegate to get the output desired in a QTableView, not sure.

            For the "detail view" (Ajouter Employe(e)) you might also like to look at Qt's QDataWidgetMapper Class.

            Jendoubi.HafedhJ Offline
            Jendoubi.HafedhJ Offline
            Jendoubi.Hafedh
            wrote on last edited by
            #5

            @JonB
            Alright, thank you so much mate.

            1 Reply Last reply
            0

            • Login

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