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. multiple access to database qt sqlite3
QtWS25 Last Chance

multiple access to database qt sqlite3

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt 5.6sqlite3
4 Posts 3 Posters 1.9k 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.
  • N Offline
    N Offline
    neko lie
    wrote on last edited by
    #1

    I have two .cpp(register_user.cpp and parking.cpp) files that will connect to a database. I tried to connect register_user.cpp to the database and it works perfectly fine. It is connected and the sql commands work fine. Tut, I need to also connect another file which is the parking.cpp to the database. I connected parking.cpp to the database and then checked for the connection. it said that it is connected but the sql commands don't work.

    How can I connect the second file to the database correctly?

    login.h

    #ifndef LOGIN_H
    #define LOGIN_H
    #include <QMainWindow>
    #include <QtSql>
    #include <QDebug>
    #include <QFileInfo>
    #include <main_interface.h>

    namespace Ui {
    class Login;
    }

    class Login : public QMainWindow
    {
    Q_OBJECT
    public:
    QSqlDatabase mydb;

    void connClose()
    {
    mydb.close();
    mydb.removeDatabase(QSqlDatabase::defaultConnection);
    }

    bool connOpen()
    {
    mydb=QSqlDatabase::addDatabase("QSQLITE");
    mydb.setDatabaseName("C:/SQLite/sqlite-tools-win32-x86-3250200/IPark.db");

    if(!mydb.open())
    {
        qDebug()<<("Failed to open database");
        return false;
    }
    else
    {
        qDebug()<<("Connected. . .");
        return true;
    }
    

    }

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

    private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    private:
    Ui::Login *ui;
    };

    #endif // LOGIN_H

    register_user.h

    #ifndef REGISTER_USER_H
    #define REGISTER_USER_H
    #include <QDialog>
    #include "login.h"

    namespace Ui {
    class register_user;
    }

    class register_user : public QDialog
    {
    Q_OBJECT

    public:
    Login conn;
    explicit register_user(QWidget *parent = 0);
    ~register_user();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::register_user *ui;
    };

    #endif // REGISTER_USER_H

    register_user.cpp

    #include <login.h>
    #include "login.h"
    #include "register_user.h"
    #include "ui_register_user.h"
    #include <QMessageBox>

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

        if(!conn.connOpen())
             ui->label_reg->setText("Failed to open database");
         else
             ui->label_reg->setText("Connected. . .");
    

    }

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

    void register_user::on_pushButton_clicked()
    {
    Login conn;
    QString username, plate_number, name;
    username=ui->lineEdit_Username->text();
    plate_number=ui->lineEdit_Plate_Number->text();
    name=ui->lineEdit_Name->text();

    QSqlQuery qry;
    qry.prepare("insert into User(User_id, plate_number, name, spot_number, credit, order_id) values('"+username+"','"+plate_number+"','"+name+"',NULL, NULL, NULL)");

    if(qry.exec()){
    QMessageBox::critical(this, tr("SAVE"), tr("SAVED"));
    }

    conn.connClose();
    }

    parking.h

    #ifndef PARKING_H
    #define PARKING_H

    #include <QDialog>
    #include "login.h"

    namespace Ui {
    class parking;
    }

    class parking : public QDialog
    {
    Q_OBJECT

    public:
    Login conn;
    explicit parking(QWidget *parent = 0);
    ~parking();

    private slots:

    void on_ParkSpace100_clicked();

    void on_ParkSpace101_clicked();

    void on_ParkSpace102_clicked();

    void on_ParkSpace103_clicked();

    private:
    Ui::parking *ui;
    };

    #endif // PARKING_H

    parking.cpp

    #include <login.h>
    #include "login.h"
    #include "parking.h"
    #include "ui_parking.h"
    #include "reservation.h"
    #include <QMessageBox>

    int status = 0;

    parking::parking(QWidget *parent):
    QDialog(parent),
    ui(new Ui::parking)
    {
    ui->setupUi(this);
    Login conn;

    if(!conn.connOpen())
    ui->label->setText("Failed to open database");
    else
    ui->label->setText("Connected. . .");

    }

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

    void parking::on_ParkSpace100_clicked()
    {

    Login conn;

    QSqlQuery qry;
    qry.prepare("insert into User(User_id, plate_number, name, spot_number, credit, order_id) values('105,0123,'Amethyst',NULL, NULL, NULL)");

    if(qry.exec()){
    QMessageBox::warning(this, tr("SAVE"), tr("SAVED"));
    }

    }

    void parking::on_ParkSpace101_clicked()
    {
    Login conn;

    QSqlQuery qry;
    conn.connOpen();

    qry.prepare("select name from User where spot_number = 101");
    bool value = qry.exec();
    if(value == true)//true : then someone is on the spot
    {
    conn.connClose();
    QMessageBox::warning(this, "Error", "Someone's already in the spot");
    qDebug("Inside ParkSpace101");
    }

    hide();
    Reservation b;
    b.setModal(true);
    b.setWindowTitle("Reservation Page");
    b.exec();

    }

    void parking::on_ParkSpace102_clicked()
    {
    QMessageBox::warning(this,"WARNING", "Someone's already in the spot.");
    }

    void parking::on_ParkSpace103_clicked()
    {
    Login conn;

    QSqlQuery qry;
    conn.connOpen();

    qry.prepare("select name from User where spot_number = 101");
    bool value = qry.exec();
    if(value == true)//true : then someone is on the spot
    {
    conn.connClose();
    QMessageBox::warning(this, "Error", "Someone's already in the spot");
    qDebug("Inside ParkSpace101");
    }

    hide();
    Reservation b;
    b.setModal(true);
    b.setWindowTitle("Reservation Page");
    b.exec();

    }

    JonBJ 1 Reply Last reply
    0
    • N neko lie

      I have two .cpp(register_user.cpp and parking.cpp) files that will connect to a database. I tried to connect register_user.cpp to the database and it works perfectly fine. It is connected and the sql commands work fine. Tut, I need to also connect another file which is the parking.cpp to the database. I connected parking.cpp to the database and then checked for the connection. it said that it is connected but the sql commands don't work.

      How can I connect the second file to the database correctly?

      login.h

      #ifndef LOGIN_H
      #define LOGIN_H
      #include <QMainWindow>
      #include <QtSql>
      #include <QDebug>
      #include <QFileInfo>
      #include <main_interface.h>

      namespace Ui {
      class Login;
      }

      class Login : public QMainWindow
      {
      Q_OBJECT
      public:
      QSqlDatabase mydb;

      void connClose()
      {
      mydb.close();
      mydb.removeDatabase(QSqlDatabase::defaultConnection);
      }

      bool connOpen()
      {
      mydb=QSqlDatabase::addDatabase("QSQLITE");
      mydb.setDatabaseName("C:/SQLite/sqlite-tools-win32-x86-3250200/IPark.db");

      if(!mydb.open())
      {
          qDebug()<<("Failed to open database");
          return false;
      }
      else
      {
          qDebug()<<("Connected. . .");
          return true;
      }
      

      }

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

      private slots:
      void on_pushButton_clicked();

      void on_pushButton_2_clicked();

      private:
      Ui::Login *ui;
      };

      #endif // LOGIN_H

      register_user.h

      #ifndef REGISTER_USER_H
      #define REGISTER_USER_H
      #include <QDialog>
      #include "login.h"

      namespace Ui {
      class register_user;
      }

      class register_user : public QDialog
      {
      Q_OBJECT

      public:
      Login conn;
      explicit register_user(QWidget *parent = 0);
      ~register_user();

      private slots:
      void on_pushButton_clicked();

      private:
      Ui::register_user *ui;
      };

      #endif // REGISTER_USER_H

      register_user.cpp

      #include <login.h>
      #include "login.h"
      #include "register_user.h"
      #include "ui_register_user.h"
      #include <QMessageBox>

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

          if(!conn.connOpen())
               ui->label_reg->setText("Failed to open database");
           else
               ui->label_reg->setText("Connected. . .");
      

      }

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

      void register_user::on_pushButton_clicked()
      {
      Login conn;
      QString username, plate_number, name;
      username=ui->lineEdit_Username->text();
      plate_number=ui->lineEdit_Plate_Number->text();
      name=ui->lineEdit_Name->text();

      QSqlQuery qry;
      qry.prepare("insert into User(User_id, plate_number, name, spot_number, credit, order_id) values('"+username+"','"+plate_number+"','"+name+"',NULL, NULL, NULL)");

      if(qry.exec()){
      QMessageBox::critical(this, tr("SAVE"), tr("SAVED"));
      }

      conn.connClose();
      }

      parking.h

      #ifndef PARKING_H
      #define PARKING_H

      #include <QDialog>
      #include "login.h"

      namespace Ui {
      class parking;
      }

      class parking : public QDialog
      {
      Q_OBJECT

      public:
      Login conn;
      explicit parking(QWidget *parent = 0);
      ~parking();

      private slots:

      void on_ParkSpace100_clicked();

      void on_ParkSpace101_clicked();

      void on_ParkSpace102_clicked();

      void on_ParkSpace103_clicked();

      private:
      Ui::parking *ui;
      };

      #endif // PARKING_H

      parking.cpp

      #include <login.h>
      #include "login.h"
      #include "parking.h"
      #include "ui_parking.h"
      #include "reservation.h"
      #include <QMessageBox>

      int status = 0;

      parking::parking(QWidget *parent):
      QDialog(parent),
      ui(new Ui::parking)
      {
      ui->setupUi(this);
      Login conn;

      if(!conn.connOpen())
      ui->label->setText("Failed to open database");
      else
      ui->label->setText("Connected. . .");

      }

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

      void parking::on_ParkSpace100_clicked()
      {

      Login conn;

      QSqlQuery qry;
      qry.prepare("insert into User(User_id, plate_number, name, spot_number, credit, order_id) values('105,0123,'Amethyst',NULL, NULL, NULL)");

      if(qry.exec()){
      QMessageBox::warning(this, tr("SAVE"), tr("SAVED"));
      }

      }

      void parking::on_ParkSpace101_clicked()
      {
      Login conn;

      QSqlQuery qry;
      conn.connOpen();

      qry.prepare("select name from User where spot_number = 101");
      bool value = qry.exec();
      if(value == true)//true : then someone is on the spot
      {
      conn.connClose();
      QMessageBox::warning(this, "Error", "Someone's already in the spot");
      qDebug("Inside ParkSpace101");
      }

      hide();
      Reservation b;
      b.setModal(true);
      b.setWindowTitle("Reservation Page");
      b.exec();

      }

      void parking::on_ParkSpace102_clicked()
      {
      QMessageBox::warning(this,"WARNING", "Someone's already in the spot.");
      }

      void parking::on_ParkSpace103_clicked()
      {
      Login conn;

      QSqlQuery qry;
      conn.connOpen();

      qry.prepare("select name from User where spot_number = 101");
      bool value = qry.exec();
      if(value == true)//true : then someone is on the spot
      {
      conn.connClose();
      QMessageBox::warning(this, "Error", "Someone's already in the spot");
      qDebug("Inside ParkSpace101");
      }

      hide();
      Reservation b;
      b.setModal(true);
      b.setWindowTitle("Reservation Page");
      b.exec();

      }

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @neko-lie
      I can't figure your code (way too long, if you want to help people to help you you need to make an effort to cut the lines you paste right down to minimal).

      You should only "connect" to the database once (it's the same database across both files, right?). Let that be Qt's "default" connection (http://doc.qt.io/qt-5/qsqldatabase.html#addDatabase, do not specify a connectionName), then that's it, any file can access the connection.

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

        Hi and welcome to devnet,

        As @JonB wrote, since you only have one database, use the default connection and configure it when starting your application.

        As written many times on this forum: don't store a local QSqlDatabase object. QSqlDatabase already provides the means to create and get back database connections when needed.

        In your code you are creating lots of Login object only to open the database connection, if you really want to open and close the connection for each and every query, then create a dedicated object for that.

        Also, don't hard code path to database like that, it's only valid on your station and furthermore, it shouldn't be in a subfolder of an installation like that. Use something like QStandardPaths to get an appropriate path.

        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
        3
        • JonBJ JonB

          @neko-lie
          I can't figure your code (way too long, if you want to help people to help you you need to make an effort to cut the lines you paste right down to minimal).

          You should only "connect" to the database once (it's the same database across both files, right?). Let that be Qt's "default" connection (http://doc.qt.io/qt-5/qsqldatabase.html#addDatabase, do not specify a connectionName), then that's it, any file can access the connection.

          N Offline
          N Offline
          neko lie
          wrote on last edited by
          #4

          @JonB
          I'm sorry, I thought it would be helpful if people saw the entirety of the code. I didn't know which part of the code I was lacking.

          Ohhh. thank you for the instruction. I'll work on the changes.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved