Qt and MySql

I have to use Qt and MySql. This is not exactely a piece of cake, expecially the first time you do that, because there is a number of prerequisite to be filled out before actually start programming.

You should have Qt and MySql installed and working on your machine - but, well, this is quite a natural requirement.

Then you have to build the plugin for MySql. This has to be done for any database you want to use with Qt but SQLite, that is provided by default with Qt.

Building a database plugin is not difficult, but imply a different procedure for each database and platform. Luckly it is well described in the official documentation.

I found that compiling and using a database plugin is a sensitive process, probabily the fact is that we have to work with specific concepts for an operating system, a compiler, a database, all of these viewed from the point of view of the Qt framework. An inerently complex environment, I reckon. So, I was not surprised my plugin did not working at first try. The Qt framework is not very helpful in let you know which is the actual problem, and why it does not like our beautiful plugin, but after a while you should usually get the point and make it working properly.

A few suggestions if you are stuck somewhere in the process: check if the database is properly installed and the required files are available to your compiler. Check if the plugin was correctly created (in the Qt plugins/sqldrivers folder). Check if the dependecies for the generated DLL are available. For Windows-Visual C++ you could use the utility depends.exe (Dependency Walker) to find that out. If you have a problem of this kind, usually is LibMySql.ddl that is not in a PATH visible from your environment.
Another DLL that is often reported missing, IESHIMS.DLL, actually is not a mandatory one, so you don't usually have to pay attention to it.

Done that, you are ready for writing code for accessing your database from Qt.

Fist thing: you have to tell to your project you are actually using the Qt database facility. This is done in the project (.pro) file, adding the word "sql" to the QT property, that should look something like that:

QT += core gui sql

If our mysql database is not up and running, well, it is better to start it up. If you are on Windows and you want do that by shell command remember you need to have the administrator rights to do that. So open your cmd shell window as administrator before giving the "net start mysql" command.

At this point, opening a connection to the database is not anymore a big issue.

Accordingly to Jasmin and Mark, this is typically done in the application main function. I wouldn't stress much this point, and I just would say that is good enough for us to do that there.

It's only a toy connection, so we don't pay much attention to security, reusability and other issues like those, and so we just write this simple function:

bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("password");
if(db.open() == false)
{
QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().databaseText());
return false;
}
return true;
}

It is worth noting that we specify the database brand calling the QSqlDatabase::addDatabase() static function, and then, after setting the usual parameters required for estabilishing a connection, we try to open it.

This function is used in this way:

#include <QtGui/QApplication>
#include <QtGui/QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>

#include "MainWindow.h"

namespace
{
bool createConnection()
{
// ...
}
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

if(createConnection() == false)
return 1;

MainWindow mw;
mw.show();

return app.exec();
}

I wrote this post while reading "C++ GUI Programming with Qt 4, Second Edition" by Jasmin Blanchette and Mark Summerfield.

No comments:

Post a Comment