PDA

View Full Version : database connection



peace_comp
12th May 2008, 16:08
Hi..
I have a trouble to connect to a mysql databse!!
I used a simple function to estblish connection in the main method :



bool createConnection(){
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setDatabaseName("projet_cpp");
db.setPassword("passwrd");
db.setHostName("localhost");
if(!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
QMessageBox::critical(0, qApp->tr(" open database"),
qApp->tr("Goood It Works.\n"
"Click ."), QMessageBox::Cancel);
return true;
}



int main(int argc,char *argv[]){
QApplication app(argc,argv);
if (!createConnection())
return 1;
..
..
}


after execution I get always the messageBox telling that conexion was failled..

I read in Qt assistant that I should add the following include:
#include "C:\Qt\4.3.4\src\sql\drivers\mysql\qsql_mysql.cpp"

but it gives only errors !!

I tried :#include "C:\Qt\4.3.4\src\sql\drivers\mysql\qsql_mysql.h"
but it gives the error :mysql.h no such file or directory within qsql_mysql.h!!!


if someone could help !!
thanks

jpn
12th May 2008, 20:09
Don't include .cpp files. See How to Build the QMYSQL Plugin on Unix and Mac OS X (http://doc.trolltech.com/4.4/sql-driver.html#how-to-build-the-qmysql-plugin-on-unix-and-mac-os-x) or How to Build the QMYSQL Plugin on Windows (http://doc.trolltech.com/4.4/sql-driver.html#how-to-build-the-qmysql-plugin-on-windows).

mazurekwrc
13th May 2008, 07:17
you forgot add

setUserName ( const QString & name )

peace_comp
13th May 2008, 12:07
hi..
I tried :
cd %QTDIR%\src\plugins\sqldrivers\mysql
qmake -o Makefile "INCLUDEPATH+=C:\MySQL\include" "LIBS+=C:\MYSQL\\lib\opt\libmysql.lib" mysql.pro
make


bu it gives the error: mysql.h no such file or directory ?
had installed mysql including libs

fo the usrName I don t think I will need it .. coze Im workin; as an admnistration d I didnt add Other users n Mysql

/?????

jpn
13th May 2008, 12:16
Yes, you need to install MYSQL to be able to build the QMYSQL plugin. You can find instructions for MinGW in our wiki: Building the QMYSQL plugin on Windows using MinGW