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. MySQL query error!
Qt 6.11 is out! See what's new in the release blog

MySQL query error!

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 706 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
    makkhay
    wrote on last edited by
    #1

    I'm trying to create a simple app which allows user to enter a word and the app will output the definition. I have compiled words and definitions by making mysql database file. I thought using MySQL would be a faster way make my app. My database looks like this:

    rowid      |  Column1(word)            | Column2(definition)
    1                   DNA                     A double-stranded
    

    I just can't figure out how to do it since I'm a beginner. So far I have tried following code:

    /#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QMessageBox>
    #include <QDebug>
    #include <QPalette>
    
    #define Path_to_DB "/Users/makkhay/Desktop/nep_eng-2.sqlite"
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    
    {
        ui->setupUi(this);
           // Set background picture
           QPixmap bkgnd("/Users/makkhay/Desktop/background.jpg");
           bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio);
           QPalette palette;
           palette.setBrush(QPalette::Background, bkgnd);
           this->setPalette(palette);
    
    
           myDB = QSqlDatabase::addDatabase("QSQLITE");
           myDB.setDatabaseName(Path_to_DB);
           QFileInfo checkFile(Path_to_DB);
    
           if(checkFile.isFile())
           {
               if(myDB.open())
               {
                 ui->label->setText("The database is connected");
               }
    
           }else{
               ui->label->setText("No file found!");
           }
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        myDB.close();
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
    //    QMessageBox::StandardButton reply= QMessageBox::question(this,
    //                                      "My Title", " Word not found, quit app?",
    //                                       QMessageBox::Yes | QMessageBox::No);
    
    
    //    if(reply == QMessageBox::Yes){
    
    //        QApplication::quit();
    //}
    
    
        if(!myDB.isOpen()){
            qDebug() << " No connection to db";
            return;
        }
    
        QString wordInput, definition;
        wordInput = ui->lineEdit->text();
    
        QSqlQuery qry;
    
        qry.prepare("SELECT Column1,Column2 FROM Nepali WHERE Column1  = :input");
        qry.bindValue(":input",wordInput);
       // int fieldNo = query.record().indexof("Column1");
    
        if(qry.exec())
        {
            ui->debug->setText(wordInput);
    
          while (qry.next())
           {
             ui->output->setText(wordInput);
            // Extract the results from the SELECT statement
            QString inputWord = qry.value(0).toString();
            QString wordDefinition = qry.value(1).toString();
            ui->output->setText(wordDefinition);
    
    
    
    
            // Then use the values for whatever you wish.
          }
        }
    jsulmJ 1 Reply Last reply
    0
    • M makkhay

      I'm trying to create a simple app which allows user to enter a word and the app will output the definition. I have compiled words and definitions by making mysql database file. I thought using MySQL would be a faster way make my app. My database looks like this:

      rowid      |  Column1(word)            | Column2(definition)
      1                   DNA                     A double-stranded
      

      I just can't figure out how to do it since I'm a beginner. So far I have tried following code:

      /#include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QMessageBox>
      #include <QDebug>
      #include <QPalette>
      
      #define Path_to_DB "/Users/makkhay/Desktop/nep_eng-2.sqlite"
      
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      
      {
          ui->setupUi(this);
             // Set background picture
             QPixmap bkgnd("/Users/makkhay/Desktop/background.jpg");
             bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio);
             QPalette palette;
             palette.setBrush(QPalette::Background, bkgnd);
             this->setPalette(palette);
      
      
             myDB = QSqlDatabase::addDatabase("QSQLITE");
             myDB.setDatabaseName(Path_to_DB);
             QFileInfo checkFile(Path_to_DB);
      
             if(checkFile.isFile())
             {
                 if(myDB.open())
                 {
                   ui->label->setText("The database is connected");
                 }
      
             }else{
                 ui->label->setText("No file found!");
             }
      
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
          myDB.close();
      }
      
      
      void MainWindow::on_pushButton_clicked()
      {
      //    QMessageBox::StandardButton reply= QMessageBox::question(this,
      //                                      "My Title", " Word not found, quit app?",
      //                                       QMessageBox::Yes | QMessageBox::No);
      
      
      //    if(reply == QMessageBox::Yes){
      
      //        QApplication::quit();
      //}
      
      
          if(!myDB.isOpen()){
              qDebug() << " No connection to db";
              return;
          }
      
          QString wordInput, definition;
          wordInput = ui->lineEdit->text();
      
          QSqlQuery qry;
      
          qry.prepare("SELECT Column1,Column2 FROM Nepali WHERE Column1  = :input");
          qry.bindValue(":input",wordInput);
         // int fieldNo = query.record().indexof("Column1");
      
          if(qry.exec())
          {
              ui->debug->setText(wordInput);
      
            while (qry.next())
             {
               ui->output->setText(wordInput);
              // Extract the results from the SELECT statement
              QString inputWord = qry.value(0).toString();
              QString wordDefinition = qry.value(1).toString();
              ui->output->setText(wordDefinition);
      
      
      
      
              // Then use the values for whatever you wish.
            }
          }
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @makkhay What is not working? What error do you get?
      You should change "MySQL query error" to "SQLite query error" since you're using SQLite and not MySQL.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        makkhay
        wrote on last edited by
        #3

        Nevermind, I solved it. Thank you for responding.

        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