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

Sqlite and QtDialog Window

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 5.3k 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.
  • 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