Solved MySQL query error!
-
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. } }
-
@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. -
Nevermind, I solved it. Thank you for responding.