在大型应用程序中使用Qt数据库

时间:2023-01-31 16:57:20

I have a question regarding using Databases in Qt.

我有一个关于在Qt中使用数据库的问题。

In other languages I would do something like create a database in the main class and pass a pointer of this database to other classes to use.

在其他语言中,我会做一些事情,比如在主类中创建一个数据库,并将该数据库的指针传递给其他要使用的类。

I have been playing around in Qt and it seems that if I initialize a database in the main class then I can just write and execute a query in any other class and it will use this database and I am a bit confused because there seems to be no reference to a database.

我一直在Qt玩,似乎如果我在主类中初始化一个数据库,那么我可以在任何其他类中编写和执行查询,它将使用这个数据库,我有点困惑,因为似乎有没有引用数据库。

Could someone explain this for me :)

有人可以帮我解释一下:)

Here is the Database class which is instanced in MainWindow.cpp

这是在MainWindow.cpp中实例化的Database类

#include "database.h"

Database::Database()
{
    Connect();
}

void Database::Connect()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("mydatabase­.dat");

    if(db.open()){
        qDebug() << "Connected";
    }
    else
    {
         qDebug() << "Not Connected";
    }
}

bool Database::SetupTables()
{
    QSqlQuery qry;

    qry.exec("CREATE TABLE patients ( patient_id INT, firstname VARCHAR(100), lastname VARCHAR(100) );  ");

    return true;
}

Here is a Window (Widget) i created called PatientList

这是我创建的一个名为PatientList的Window(Widget)

#include "patientlist.h"
#include "ui_patientlist.h"
#include "database.h"
#include <QtSql>
#include <QtDebug>

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

    QSqlQuery qry;
    if(qry.exec("SELECT patient_id,firstname,lastname FROM patients")){
        qDebug() << "Success";
    }
    else
    {
        qDebug() << "Error";
    }

}

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

This all works fine but I just feel like im doing something wrong because I have not mentioned the database created in the MainWindow.cpp

这一切都很好但我只是觉得我做错了,因为我没有提到在MainWindow.cpp中创建的数据库

2 个解决方案

#1


2  

From Qt documentation on QSqlDatabase:

从QSqlDatabase上的Qt文档:

A connection is known by its own name, not by the name of the database it connects to. You can have multiple connections to one database. QSqlDatabase also supports the concept of a default connection, which is the unnamed connection. To create the default connection, don't pass the connection name argument when you call addDatabase(). Subsequently, when you call any static member function that takes the connection name argument, if you don't pass the connection name argument, the default connection is assumed.

连接以其自己的名称而不是它所连接的数据库的名称而为人所知。您可以与一个数据库建立多个连接。 QSqlDatabase还支持默认连接的概念,即未命名的连接。要创建默认连接,请在调用addDatabase()时不要传递连接名称参数。随后,当您调用任何采用连接名称参数的静态成员函数时,如果未传递连接名称参数,则假定使用默认连接。

Well, I think this says it all. Note that QSqlDatabase cannot be directly instantiated. Direct creating instance of QSqlDatabase will only produce invalid connection. Apparently a QSqlDatabase are only links to actual connection objects (probably allocated statically). As many applications are likely to use only one database connection, one link is singled-out as default.

嗯,我认为这说明了一切。请注意,无法直接实例化QSqlDatabase。直接创建QSqlDatabase实例只会产生无效连接。显然,QSqlDatabase只是指向实际连接对象的链接(可能是静态分配的)。由于许多应用程序可能只使用一个数据库连接,因此默认情况下会挑出一个链接。

I am unsure if this is done on per-application or per-thread basis.

我不确定这是基于每个应用程序还是每个线程完成的。

#2


0  

Well not seeing your main makes it difficult but it looks like QSqlQuery is smart enough to pick up your "default" database. See here:

好吧,没有看到你的主要困难,但看起来QSqlQuery足够智能,可以拿起你的“默认”数据库。看这里:

http://qt-project.org/doc/qt-4.8/qsqlquery.html#QSqlQuery-2

http://qt-project.org/doc/qt-4.8/qsqlquery.html#QSqlQuery-2

So as long as you're seeing values in your db, I'd say it's just another example of Qt doing something not necessarily intuitive but ultimately very easy to use.

因此,只要您在数据库中看到值,我就会说这只是Qt的另一个例子,它做的事情不一定直观但最终很容易使用。

#1


2  

From Qt documentation on QSqlDatabase:

从QSqlDatabase上的Qt文档:

A connection is known by its own name, not by the name of the database it connects to. You can have multiple connections to one database. QSqlDatabase also supports the concept of a default connection, which is the unnamed connection. To create the default connection, don't pass the connection name argument when you call addDatabase(). Subsequently, when you call any static member function that takes the connection name argument, if you don't pass the connection name argument, the default connection is assumed.

连接以其自己的名称而不是它所连接的数据库的名称而为人所知。您可以与一个数据库建立多个连接。 QSqlDatabase还支持默认连接的概念,即未命名的连接。要创建默认连接,请在调用addDatabase()时不要传递连接名称参数。随后,当您调用任何采用连接名称参数的静态成员函数时,如果未传递连接名称参数,则假定使用默认连接。

Well, I think this says it all. Note that QSqlDatabase cannot be directly instantiated. Direct creating instance of QSqlDatabase will only produce invalid connection. Apparently a QSqlDatabase are only links to actual connection objects (probably allocated statically). As many applications are likely to use only one database connection, one link is singled-out as default.

嗯,我认为这说明了一切。请注意,无法直接实例化QSqlDatabase。直接创建QSqlDatabase实例只会产生无效连接。显然,QSqlDatabase只是指向实际连接对象的链接(可能是静态分配的)。由于许多应用程序可能只使用一个数据库连接,因此默认情况下会挑出一个链接。

I am unsure if this is done on per-application or per-thread basis.

我不确定这是基于每个应用程序还是每个线程完成的。

#2


0  

Well not seeing your main makes it difficult but it looks like QSqlQuery is smart enough to pick up your "default" database. See here:

好吧,没有看到你的主要困难,但看起来QSqlQuery足够智能,可以拿起你的“默认”数据库。看这里:

http://qt-project.org/doc/qt-4.8/qsqlquery.html#QSqlQuery-2

http://qt-project.org/doc/qt-4.8/qsqlquery.html#QSqlQuery-2

So as long as you're seeing values in your db, I'd say it's just another example of Qt doing something not necessarily intuitive but ultimately very easy to use.

因此,只要您在数据库中看到值,我就会说这只是Qt的另一个例子,它做的事情不一定直观但最终很容易使用。