Talon06
Talon06

Reputation: 1796

C++ QT SQLite Best Practice

I am building an app that writes data to a database and reads it. I need to know the best way to go about doing it. Firstly will it slow down my application setting up functions that open and close the database when they run?

void Inventory::insert(int item, double qnty, int loc)
{
CC_Number cn;
char result[100];   // array to hold the result.
strcpy(result,"INSERT INTO Inventory (Item_ID, Qnty , Loc_ID) Values (");
strcat(result,cn.int_to_char(1));
strcat(result,",");
strcat(result,cn.int_to_char(2));
strcat(result,",");
strcat(result,cn.int_to_char(3));
strcat(result,");");
db = new CC_Database("Inventory.sqlite");
db->query(result);
db->close();
}

Or should I open the database when the main window of the app opens and close the database when it is destroyed?

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
db = new CC_Database("Inventory.sqlite");
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
db->close();
delete ui;
}

I started out with the second method, but hit a road block getting the db variable from the mainwindow class into other classes in order to run queries. I am very new to C++ so if I have made a glaring mistake please be kind.

Upvotes: 1

Views: 1574

Answers (2)

tanius
tanius

Reputation: 16769

Or should I open the database when the main window of the app opens and close the database when it is destroyed? […]

Yes, that will be the preferred method because there is simply no need to open and close a database connection all the time.

I [… tried that] but hit a road block getting the db variable from the mainwindow class into other classes in order to run queries.

In the Qt SQL classes, there is a simple solution for this when you are only using a single database in your application: by creating an unnamed database connection, that connection will be used by default when you use QSqlQuery::exec() without specifying a connection (manual ref).

So you would indeed "open the database when the main window of the app opens and close the database when it is destroyed", like this:

MainWindow::MainWindow(QWidget *parent) 
    : QMainWindow(parent), ui(new Ui::MainWindow) {

    db = QSqlDatabase::addDatabase("QSQLITE"); // QSqlDatabase
    db.setDatabaseName("Inventory.sqlite");

    ui->setupUi(this);
}

MainWindow::~MainWindow() {
    db->close();
    delete ui;
}

And then any code that wants to access the database with a query can simply look like this:

QSqlQuery query;
query.prepare("INSERT INTO Inventory ...");
if(!query.exec()) {
    qWarning() << "ERROR: " << query.lastError().text();
}

For a full example of using SQLite this way under Qt, have a look at QtSqlExample or QML SQL Example. (The latter is my own variant, using QML. The first uses QWidgets instead.)

Upvotes: 0

Andrey Tuganov
Andrey Tuganov

Reputation: 361

I would use singleton design pattern. This way the database would be opened when needed and accessible from everywhere.

Upvotes: 3

Related Questions