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. How to get Data from MySQL and set it in a Label?
Forum Updated to NodeBB v4.3 + New Features

How to get Data from MySQL and set it in a Label?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 1.1k 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.
  • M Offline
    M Offline
    meepo1
    wrote on last edited by meepo1
    #1

    Here i show my code

    It Does not work because it returns nothing and my Database is not empty.
    I got a message from Qt, it says

    QSqlQuery::value: not positioned on a valid record
    .

    #include "Wind0.h"
    #include "ui_Wind0.h"
    #include <QSqlTableModel>
    #include <QMessageBox>
    #include <QSqlError>
    #include <QSqlQuery>
    #include <QDebug>
    #include <QSqlRecord>
    
    Wind0::Wind0(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Wind0)
    {
        ui->setupUi(this);
    
        mDatabase = QSqlDatabase::addDatabase("QMYSQL");
        mDatabase.setHostName("localhost");
        mDatabase.setDatabaseName("BD1");
        mDatabase.setPort(3306);
        mDatabase.setUserName("root");
        mDatabase.open();
    }
    
    Wind0::~Wind0()
    {
        delete ui;
    }
    void Wind0::on_pushButton_clicked()
    {
        QSqlQuery query;
        QString aux;
        query.prepare("select Name from Teacher");
        aux = query.value(1).toString ;
        ui->label->setText(aux);
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      meepo1
      wrote on last edited by meepo1
      #2

      I got the solution :) I added exec and first

      void Wind0::on_pushButton_clicked()
      {
          QSqlQuery query;
          QString aux;
          query.prepare("select Name from Teacher");
          query.exec();
          query.first();
          aux = query.value(0).toString ;
          ui->label->setText(aux);
         if(!query.isActive()) {
          QMessageBox::information(0,"Bad Query", "BAD Query. It's not active");
      }
      

      Like

      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