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. Sqlite and QtDialog Window
Forum Updated to NodeBB v4.3 + New Features

Sqlite and QtDialog Window

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 3.5k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    andrewhopps
    wrote on last edited by
    #1

    I am trying to interact with an sql database through my dialog box. I make the connection, but when I try to run a query, nothing happens. I have a mainwindow source and header, a dialog source and header, and main source. I tried to follow a youtube tutorial, but he did it on his main window, so I figure I have includes or something in the wrong place. In the following order: car.h, car.cpp, mainwindow.h, mainwindow.cpp, main.cpp

    @#ifndef CAR_H
    #define CAR_H

    #include <QDialog>
    #include <QtSql>
    #include <QtDebug>
    #include <QFileInfo>

    namespace Ui {
    class Car;
    }

    class Car : public QDialog
    {
    Q_OBJECT

    public:
    explicit Car(QWidget *parent = 0);
    ~Car();

    private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();
    

    private:
    Ui::Car *ui;
    QSqlDatabase mydb;
    };

    #endif // CAR_H
    @

    @#include "car.h"
    #include "ui_car.h"
    #include <QMessageBox>

    Car::Car(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Car)
    {
    ui->setupUi(this);
    mydb = QSqlDatabase::addDatabase("QSQLITE");
    mydb.setDatabaseName("RacingInfo.sqlite"); //Path

    if(!mydb.isOpen())
        ui->label_4->setText("Failed to open the database.");
    else
        ui->label_4->setText("Successfully opened database.");
    

    }

    Car::~Car()
    {
    delete ui;
    }

    void Car::on_pushButton_clicked()
    {
    QString cartype,carnumber;
    cartype=ui->lineEdit_type->text();
    carnumber=ui->lineEdit_number->text();

    if(!mydb.isOpen())
    {
        qDebug()<<"Failed to open database.";
        return;
    }
    
    QSqlQuery qry;
    
    if(qry.exec&#40;"select * from car where type='"+cartype +"' and number='"+carnumber+"'"&#41;&#41;
    {
        int count=0;
        while(qry.next(&#41;&#41;
        {
            count++;
        }
        if(count==1)
            ui->label_caradded->setText("Already Exists.");
        if(count>1)
            ui->label_caradded->setText("Duplicate Entries Already Found.");
        if(count<1)
            ui->label_caradded->setText("Successfully Added!");
    }
    

    }

    void Car::on_pushButton_2_clicked()
    {
    Car::reject();
    }@

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_2_clicked();

    void on_exitButton_clicked();
    
    void on_addcarButton_clicked();
    

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "car.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_2_clicked()
    {

    }

    void MainWindow::on_exitButton_clicked()
    {

    }

    void MainWindow::on_addcarButton_clicked()
    {
    Car car;
    car.setModal(true);
    car.exec();
    }
    @

    @#include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec(&#41;;
    

    }
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You are checking that the query succeeds but you don't do anything if it fails. Did you verify what lastError returns ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andrewhopps
        wrote on last edited by
        #3

        !http://i.imgur.com/cHlCccY.png(Error and proof that table exists)!

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andrewhopps
          wrote on last edited by
          #4

          I seem to have .isOpen and .open used interchangeably throughout my code.... I have since moved all database creation to the mainwindow as it is initialized and I currently don't have the failed to open database error.

          Will reply with whether or not query works when I get there.

          But question:

          1. Do I need to include QtSql in all headers of windows that use SQL?
          2. How do I access my database from the other windows?
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Don't include QtSql, just the headers of the class you are using.

            Once the connection is done (if you are using the default connection) just use QSqlQuery and friends where needed

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andrewhopps
              wrote on last edited by
              #6

              @ QSqlDatabase db;
              db = QSqlDatabase::addDatabase("QSQLITE", "connection-name");
              db.setDatabaseName("RacingInfo.sqlite"); //Path@

              Is in my mainwindow.cpp initialization, I am unsure what exact code I should be using to access the database from another window/dialog. The second window is Modal if that makes any difference. I have tried many times with cut and paste code, but I continue to fail to connect to database.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Just use

                @QSqlDatabase db = QSqlDatabase::database("connection-name");@

                And reuse that db object in you query. Or if you only have one connection, don't set any name and you don't need to call QSqlDatabase::database. QSqlQuery for example uses the default connection if you don't specify otherwise

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andrewhopps
                  wrote on last edited by
                  #8

                  @void Car::on_pushButton_clicked()
                  {
                  QString cartype,carnumber;
                  cartype=ui->lineEdit_type->text();
                  carnumber=ui->lineEdit_number->text();

                  QSqlDatabase db = QSqlDatabase::database();
                  QSqlQuery query(db);
                  
                  if(!db.isOpen())
                  {
                      qDebug()<<"Failed to open database.";
                      return;
                  }
                  else{
                  if(query.exec&#40;"SELECT * FROM Car"&#41;)
                      ui->label_caradded->setText("Working");
                  else
                      qDebug() << query.lastError();
                  }
                  

                  }@

                  The database appears to be open in the second window as I have set an image to display if it is open when the window is initialized. I'm sorry, I am having such a hard time grasping this. But the database spits out, QSqlError(1, "Unable to execute statement", "no such table: Car") . No matter what table I put it or make, it doesn't change.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @ db.setDatabaseName("RacingInfo.sqlite"); //Path@

                    Where is "RacingInfo.sqlite" located ? It's a relative path so the application will try to open it in the same folder where the executable is located. I suspect that it's currently not the case.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andrewhopps
                      wrote on last edited by
                      #10

                      @#include "mainwindow.h"
                      #include "ui_mainwindow.h"
                      #include "car.h"
                      #include "track.h"
                      #include "about.h"
                      #include "documentation.h"
                      #include "viewtable.h"
                      #include "racetype.h"
                      #include "addnew.h"

                      MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                      {
                      ui->setupUi(this);

                      QSqlDatabase db;
                      db = QSqlDatabase::addDatabase("QSQLITE");
                      db.setDatabaseName("C:/Users/Andrew/Racing/RacingInfo.sqlite"); //Path
                      
                      if(!db.open())
                      {
                          QPixmap pix(":/Resources/Images/Resources/Images/CircleRed.png");
                          ui->status->setPixmap(pix);
                          qDebug()<<"Failed to open database.";
                          return;
                      }
                      else{
                          QPixmap pix(":/Resources/Images/Resources/Images/CircleGreen.png");
                          ui->status->setPixmap(pix);
                          qDebug()<<"I think database is open.";
                      }
                      

                      }

                      MainWindow::~MainWindow()
                      {
                      delete ui;
                      }

                      void MainWindow::on_pushButton_2_clicked()
                      {
                      QSqlDatabase db = QSqlDatabase::database();
                      QSqlQuery query(db);
                      if(!query.exec("SELECT * FROM Car"))
                      qDebug()<<query.executedQuery();
                      else
                      QMessageBox::information(this,tr("Database"),tr("Is working!");

                      }@

                      !http://i.imgur.com/wFcmzPi.png(After clicking button, this is what qDebug spits out.)!

                      qDebug spits out "" every single time, I have multiple copies of my database in every possible location: where qt source files are, in the debug build folder, and one inside the debug folder inside that and I am not getting any info back. I noticed that there was a file called pRacingInfo.sqlite I don't know what that is. Following down my code, qDebug says my database is open when I initialize the window, but once I click the button I get the "".

                      So I cannot technically access it from my main window, let alone the others.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        When you try to open a database with sqlite and the file doesn't exist it will be created if possible. That's why the opening always succeeds in your case.

                        So just ensure that you have the correct file at the correct place when opening it.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        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