PDA

View Full Version : Mysterious Qt4.2.0 Mysql 5.0.41 connection problem



locus
23rd June 2007, 03:56
I am running two peices of code shown below, both of which are trying to connect to the mysql server (version 5.0.41).

The codes are shown below; one is basically the carbon copy of the other, they are running on the same machine, same operating system.

I am using MVS .NET 2003

The first code (code number one), well thats all there is to it, just a main.cpp file.

The only difference between the two peices of code is that the last one (code number two), is a part of a large project (and as you can see i have commented the inclusion of other files in the project, in an attempt to get the two peices of code exactly alike).

I am using QMessageBox to test the outputs and see whats going on.

From code number one i get a message box telling me that i am connected to the server.

From code number two i get a message box saying : DRIVER NOT LOADED DRIVER NOT LOADED.

If you have any suggestions whatsoever that could explain this, please post.

Thanks in advance for your time.

Here they are:

code number one:



#include <qapplication.h>
#include<QtCore>
#include<QtGui>
#include <QSqlDatabase>
#include <QSqlError>
#include<QString>





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


int main( int argc, char** argv )
{
QApplication app( argc, argv );
const QString stry = "connected";
if(createConnection()){
QMessageBox::information(0, QObject::tr("Database Error"),stry);
} // create database connections



return 0;
}



code number two


#include <QApplication.h>
#include<QtCore>
#include<QtGui>
#include <QSqlDatabase>
#include <QSqlError>
#include<QString>
#include<QStringList>
//#include "global.h"
//#include "mainConsole.h"
//#include "pabxdialog.h"



//bool USER_FLAG; // USER_FLAG GLOBAL VARIABLE ** defined elsewhere as extern **



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


int main( int argc, char** argv )
{
QApplication app( argc, argv );
const QString stry = "connected";
//const QString stry1 = "driver not found";



if(createConnection()){
QMessageBox::information(0, QObject::tr("Database Error"),stry);
} // create database connections
//mainConsole pabx( 0, 0,0, &app ); // instantiate main console



return 0;
}

jacek
24th June 2007, 18:00
The only difference between the two peices of code is that the last one (code number two), is a part of a large project
There must be some other difference that causes the problem. Do you build those two projects in the same way?

locus
24th June 2007, 21:35
Thats what i am saying, so i'm trying to find out what the difference is.

I don't know how relevant these values are to the build, but here are some values that are the same with both projects in the MSVS .NET 2003 environment:

Project Property Pages:

Code Generation -> runtime library -> Multithreaded debug Dll (/MDd)

Linker input -> additional dependencies -> QtSqld4.lib QtCored4.lib ...... (all the libraries here are the same for both projects)

command line options are also the same.

i looked through the build log trying to find a difference, there seem to be no significant difference that could explain the problem.

All qt modules have been included in both projects.


By adding the piece of code below, to code number two (the one giving the driver not loaded problem, i realize that it is only seeing one driver as available.

A little bit more code reveils that the only driver it picks up is the QSQLITE.


QStringList drivers = QSqlDatabase::drivers();

int str_int = drivers.count();

QVariant var(str_int);

const QString stry3 = var.toString();

QMessageBox::information(0, QObject::tr("Database Error"),stry3);

Thanks a lot for your response, i am still looking.

The problem is i am running out of differences to look for.

If there is anything you can suggest please, feel free.

Thanks again.

jacek
27th June 2007, 17:38
Maybe you should take a look at the contents of directories where the executables are?

mcosta
27th June 2007, 22:11
The libmysql.dll with "MySQL 5.0.41" seems not work fine with Qt 4.x.
I resolved installing MySQL 5.0.37.

Sorry for my very BAD English
:)

locus
4th July 2007, 18:59
Thanks mcosta for your response.

I got it to work though.

Jacek was right, the dll for the sql module was inside my project excecutable directory. Along with some other qt dlls. Can't quite remember why i put them there.

I think i was trying to get the program to run as a stand alone, ie. by just clicking on the .exe file, without opening the application in visual studio.

But i remove the dll and everything worked like a charm.

MCOSTA: The dll works fine, as long as it gets built correctly, if you ever have problems with that you can send me a PM and i'll post you a guide.