Liam
Liam

Reputation: 105

Using Qt Database in Large Application

I have a question regarding using Databases in 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.

Could someone explain this for me :)

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

#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

#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

Upvotes: 1

Views: 2823

Answers (2)

j_kubik
j_kubik

Reputation: 6181

From Qt documentation on QSqlDatabase:

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.

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.

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

Upvotes: 2

cppguy
cppguy

Reputation: 3713

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

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.

Upvotes: 0

Related Questions