[Qt-interest] Using a single MySQL Connection in the whole project

Dan White ygor at comcast.net
Wed Aug 4 17:12:57 CEST 2010


Wrap it in a Singleton.

<http://www.yolinux.com/TUTORIALS/C++Singleton.html>

I did that once in a system that connected to Oracle.

“Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.”
Bill Waterson (Calvin & Hobbes)

----- David Villalobos Cambronero <david.villalobos.c at gmail.com> wrote:
> Hi all,
> 
> My name is David, I'm really new in Qt.
> 
> I'm writing an application to learn it, but I have found a problem
> while trying to connect to MySQL.
> 
> Here is the scenario:
> 
> I have a MDI Widget where I'll add some classes with information, i.e
> a widget showing the MySQL server status an other to show the MySQL
> Variables.
> 
> I can connect successfully to MySQL with the following statement:
> 
> QSqlDatabase mainServerConnection = QsqlDatabase::addDatabase("QMYSQL");
> 
> In addition I have a class that shows a form for collecting the
> connection parameters.
> 
> But, How can I use the mainServerConnection in other classes, I have
> tried to make it public, but the addDatabase seems not to work. Here
> is what I tried:
> 
> In MainClass.h
> public:
>   QSqlDatabase *mainServerConnection;
> 
> In MainClass.cpp
> mainServerConnection = new QSqlDatabase;
> mainServerConnection->addDatabase("QMYSQL");
> 
> How can I use a just one MySQL Connection in my whole project?
> 
> And many thanks for the help.
> 
> PD: BTW I'm not American, so my English is bad, please be patient.
> 
> Best regards
> David
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest





More information about the Qt-interest-old mailing list