[Qt-interest] Design of Qt/DB applications
Jason H
scorp1us at yahoo.com
Wed Jan 27 02:42:44 CET 2010
Once you connect to a database, unless you specify the database as a parameter, it will use the connected database.
Your only problem is that that "db" goes away. If you have only one connection for your app, put it in your main() or QMainDialog-derived instance.
________________________________
From: William Lugg <wlugg at prairienetworks.com>
To: qt-interest at trolltech.com
Sent: Tue, January 26, 2010 8:37:58 PM
Subject: [Qt-interest] Design of Qt/DB applications
OK, here's what may be a duh question.
I've been trying to study the SQL examples and figure that out, but have failed. I'm working on an application that will provide a rather elaborate membership roster for an organization. It is using SQLite for the DB engine and Qt (obviously) for the GUI. I've build a simple class to handle the connection of the app to the DB and got the impression from the examples that I should place a call to the static connection::createConnection() in main.cpp before I started the app itself. However, after some debugging, I'm discovering that it looks like I need the createConnection call in each form and dialog I open that needs access to the DB. Is the correct or am I not doing the right things in my createConnection code? Here's the code if that helps...
bool Connection::createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "rlhs_db");
db.setDatabaseName("../database/rlhs_db");
if (db.open())
{
return true;
}
else
{
QMessageBox::critical(0,
qApp->tr( "Cannot open database"),
qApp->tr( "Unable to establish a database connection.\n"
"This application needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."),
QMessageBox::Cancel);
return false;
}
}
Looks a lot like the example code, doesn't it? :o)
Thanks for the help.
----
Bill Lugg
Milstar Software Support
Peterson AFB, CO
No Micro$oft products were used to create this email.
God rules and if He needed a computer it would run Linux!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100126/22fc0f5b/attachment.html
More information about the Qt-interest-old
mailing list