多次访问数据库qt sqlite3

时间:2018-11-02 09:50:33

标签: qt sqlite

免责声明:很抱歉,如果代码没有整齐地缩进。知道有些人对此不满意。 我正在使用qt5

我有两个.cpp(register_user.cpp和parking.cpp)文件将连接到数据库。我尝试将register_user.cpp连接到数据库,并且工作正常。它已连接,并且sql命令可以正常工作。 Tut,我还需要将另一个文件parking.cpp连接到数据库。我将parking.cpp连接到数据库,然后检查连接。它说它已连接,但是sql命令不起作用。

如何将第二个文件正确连接到数据库?

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

}

1 个答案:

答案 0 :(得分:0)

您将希望将唯一的connectionName传递到QSqlDatabase::addDatabase()的{​​{1}}方法的connOpen中的第二个参数中。这样一来,您就可以在保持旧连接的同时添加新的数据库连接。

因此,带有login.h的行类似于

addDatabase()

请确保mydb=QSqlDatabase::addDatabase("QSQLITE", your_connection_name_here); connectionName的{​​{1}}不同。我的建议是将register_user作为参数传递到parking的构造函数中。然后将其存储到成员变量中。

connectionName

并修改QSqlDatabase函数以处理成员变量

Login

explicit Login(const QString &connectionName, QWidget *parent = 0); mydb.removeDatabase(m_connectionName); ... mydb=QSqlDatabase::addDatabase("QSQLITE", m_connectionName); 中启动登录时,您可以传递自己的连接名称。

register_user

如果您不喜欢将参数传递给构造函数,则另一种方法是为connectionName使用setter函数。

parking

然后在Login conn("register_user_sqlite_connection"); void setConnectionName(const QString &connectionName); 中启动登录后立即调用它。

register_user

进一步阅读:http://doc.qt.io/qt-5/qsqldatabase.html#addDatabase


编辑:

OP代码不起作用的最小示例

parking