From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.


multiple access to database qt sqlite3



  • 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();

    }



  • @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.


  • Lifetime Qt Champion

    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.



  • @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.


Log in to reply