Home > front end >  How do I assign a value from a SQL query to a variable? QT
How do I assign a value from a SQL query to a variable? QT

Time:01-04

void Registration::introductionDate(QString email){
    QSqlQuery *query = new QSqlQuery();
    int dailyCalorieIntake = query->prepare("SELECT dailyCalorieIntake FROM [dbo].[User] WHERE email = :email");
    query->bindValue(":email", email);
    int dailyProteinIntake = query->prepare("SELECT dailyProteinIntake FROM [dbo].[User] WHERE email = :email");
    query->bindValue(":email", email);
    int dailyIntakeOfCarbohydrates = query->prepare("SELECT dailyIntakeOfCarbohydrates FROM [dbo].[User] WHERE email = :email");
    query->bindValue(":email", email);
   int dailyIntakeOfFats = query->prepare("SELECT dailyIntakeOfFats FROM [dbo].[User] WHERE email = :email");
    query->bindValue(":email", email);
    float bmi = query->prepare("SELECT bmi FROM [dbo].[User] WHERE email = :email");
    query->bindValue(":email", email);
    if (!query->exec())
       {
           QMessageBox::critical(this, "Programm", query->lastError().text());
       }

    days->getUi().proteinsEaten->setText("0/"   QString::number(dailyProteinIntake));
    days->getUi().calorieEaten->setText("0/"   QString::number(dailyCalorieIntake));
    days->getUi().carbohydratesEaten->setText("0/"   QString::number(dailyIntakeOfCarbohydrates));
    days->getUi().fatsEaten->setText("0/"   QString::number(dailyIntakeOfFats));
    days->getUi().bmiValue->setText("0/"   QString::number(bmi));
}

I need to assign some value to my variable using QSqlQuery. The value can be in the database. From there I need to take the value and assign it to my variable. How to do it?

CodePudding user response:

The value can be in the database From there I need to take the value and assign it to my variable. How to do it?

use QVariant QSqlQuery::value(int index) const or QVariant QSqlQuery::value(const QString &name) const here is what i find qsqlquery class

here is the code:

    QSqlQuery* query = new QSqlQuery();
    query->prepare("SELECT dailyCalorieIntake, dailyProteinIntake,dailyIntakeOfCarbohydrates,dailyIntakeOfFats,bmi FROM [dbo].[User] WHERE email = :email");
    query->bindValue(":email", email);
  
    if (!query.next())
    {
        return;
        //QMessageBox::critical(this, "Programm", query->lastError().text());
    }
    int dailyCalorieIntake = query.value(0).toInt();
    int dailyProteinIntake = query.value(1).toInt();
    int dailyIntakeOfCarbohydrates= query.value(2).toInt();
    int dailyIntakeOfFats = query.value(3).toInt();
    float bmi = query.value(4).toFloat();

CodePudding user response:

void Registration::introductionDate(QString email, Days *daysArg){

    QSqlQuery *query = new QSqlQuery(daysArg->getDataBaseKnowFood());
   query->prepare("SELECT dailyCalorieIntake FROM [dbo].[User] WHERE email = :email");
    query->bindValue(":email", email);
    if (!query->exec())
       {
           QMessageBox::critical(this, "Programm", query->lastError().text());
       }
    query->first();
     float dailyCalorieIntake = query->value("dailyCalorieIntake").toFloat();
 daysArg->getUi().calorieEaten->setText("0/"   QString::number(dailyCalorieIntake));
 QSqlQuery *query2 = new QSqlQuery(daysArg->getDataBaseKnowFood());
   query2->prepare("SELECT dailyProteinIntake FROM [dbo].[User] WHERE email = :email");
    query2->bindValue(":email", email);
    if (!query2->exec())
       {
           QMessageBox::critical(this, "Programm", query2->lastError().text());
       }
    query2->first();
     float dailyProteinIntake = query2->value("dailyProteinIntake").toFloat();
     daysArg->getUi().proteinsEaten->setText("0/"   QString::number(dailyProteinIntake));
QSqlQuery *query3 = new QSqlQuery(daysArg->getDataBaseKnowFood());
     query3->prepare("SELECT dailyIntakeOfCarbohydrates FROM [dbo].[User] WHERE email = :email");
    query3->bindValue(":email", email);
    if (!query3->exec())
       {
           QMessageBox::critical(this, "Programm", query3->lastError().text());
       }
    query3->first();
     float dailyIntakeOfCarbohydrates = query3->value("dailyIntakeOfCarbohydrates").toFloat();

      daysArg->getUi().carbohydratesEaten->setText("0/"   QString::number(dailyIntakeOfCarbohydrates));
QSqlQuery *query4 = new QSqlQuery(daysArg->getDataBaseKnowFood());
   query4->prepare("SELECT dailyIntakeOfFats FROM [dbo].[User] WHERE email = :email");
    query4->bindValue(":email", email);
    if (!query4->exec())
       {
           QMessageBox::critical(this, "Programm", query4->lastError().text());
       }
    query4->first();
     float dailyIntakeOfFats = query4->value("dailyIntakeOfFats").toFloat();

     daysArg->getUi().fatsEaten->setText("0/"   QString::number(dailyIntakeOfFats));
QSqlQuery *query5 = new QSqlQuery(daysArg->getDataBaseKnowFood());
     query5->prepare("SELECT bmi FROM [dbo].[User] WHERE email = :email");
    query5->bindValue(":email", email);
    if (!query5->exec())
       {
           QMessageBox::critical(this, "Programm", query5->lastError().text());
       }
    query5->first();
     float bmi = query5->value("bmi").toFloat();

   daysArg->getUi().bmiValue->setText(QString::number(bmi));

  QSqlQuery *query6 = new QSqlQuery(daysArg->getDataBaseKnowFood());
        query6->prepare("SELECT login FROM [dbo].[User] WHERE email = :email");
       query6->bindValue(":email", email);
       if (!query6->exec())
          {
              QMessageBox::critical(this, "Programm", query6->lastError().text());
          }
     query6->first();
      QString login = query6->value("login").toString();
      daysArg->getUi().usernameInfo->setText(login);
  }
  •  Tags:  
  • c qt
  • Related