PDA

View Full Version : Problem Connecting with MySQL in a commandline application



Anas
2nd April 2012, 18:38
Hi Guys,

I am working on a small command line application in which I need basic MySQL connectivity. I am using Qt Creator 2.4.1 with Qt 4.7.4 on Windows 7. I created a "Qt Console Application" project and tried to compile the following trivial program.


#include <QtCore/QCoreApplication>
#include <QtSql/QSqlDatabase>

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase myDb = QSqlDatabase::addDatabase( "QMYSQL" );

myDb.setHostName( "localhost" );
myDb.setUserName( "root" );
myDb.setPassword( "*********" );
myDb.setDatabaseName( "mytestdb" );

bool success = myDb.open();

if ( success ) cout << "Database connection established successfully" << endl;
else cout << "Database connection failed" << endl;

myDb.close();

QSqlDatabase::removeDatabase( myDb.database().connectionName() );

return a.exec();
}


On building this project, I get following, about 18 errors.

7552
Please tell me what am I doing wrong and where?
Thanks!!!

ChrisW67
3rd April 2012, 00:06
QT += sql

missing from your PRO file would be my first guess.

Anas
3rd April 2012, 20:56
Thanks! after adding this line to the project file, it at least compiles successfully. Now I have discovered another big blunder. i.e. I have not built and loaded QMYSQL driver. I am trying to do this but the instructions in this page of QT documentation (http://doc.qt.nokia.com/4.7-snapshot/sql-driver.html#qmysql) don't seem to be very precise in my case. I have installed MySQL 5.5 using windows installer bundle which installed not only MySQL server but also MySQL workbench and many connectors such as C/C++, .Net, Java etc. Also I installed QtSDK which installed everything including Qt Creator etc. Now the problem is that directory structure on my computer is not exactly as given in Qt docs.
1) Qt docs instructs to go to C:\MySQL\MySQL51\lib\opt while I can't find a corresponding \opt\ folder in this directory of my installation: C:\Program Files\MySQL\MySQL Server 5.5\lib . However a file named libmysql.lib is present in C:\Program Files\MySQL\MySQL Server 5.5\lib folder. Could this folder be used in place of C:\MySQL\MySQL51\lib\opt.?? The only \opt\ folders that I have found yet are in these directories: C:\Program Files\MySQL\Connector C 6.0.2\lib and C:\Program Files\MySQL\Connector C++ 1.1.0\lib but none of these \opt\ folders contain any file named libmysql.lib.
2) Next, however, I am able to find this folder in my installation: C:\Program Files\MySQL\MySQL Server 5.5\include which I think corresponds to C:\MySQL\MySQL51\include mentioned in the documentation. Am I right?
3) At verious points, documentation instructs to set values for path system environment variable. If I am using the terminal from Qt's start menu shortcuts then is it still necessary to set these values for "path"?
4) I don't have any C:\Qt\4.6.2 folder instead there is a configure.exe file in the folder C:\QtSDK\Desktop\Qt\4.8.0\mingw . Is it the same as C:\Qt\4.6.2 ?
Please guide me in making these decision because I am really bad at working in command line :(

ChrisW67
4th April 2012, 00:50
1) Qt docs instructs to go to C:\MySQL\MySQL51\lib\opt while I can't find a corresponding \opt\ folder in this directory of my installation: C:\Program Files\MySQL\MySQL Server 5.5\lib . However a file named libmysql.lib is present in C:\Program Files\MySQL\MySQL Server 5.5\lib folder. Could this folder be used in place of C:\MySQL\MySQL51\lib\opt.??
Yes, I expect so.

2) Next, however, I am able to find this folder in my installation: C:\Program Files\MySQL\MySQL Server 5.5\include which I think corresponds to C:\MySQL\MySQL51\include mentioned in the documentation. Am I right?

Sounds good

3) At verious points, documentation instructs to set values for path system environment variable. If I am using the terminal from Qt's start menu shortcuts then is it still necessary to set these values for "path"?

Yes, you are adding the separate copy of the MingW utilities to the PATH in your command shell. These utilities are used in addition to the ones bundled in the SDK. You need this particularly for the reimp tool that is not bundled in the SDK.


4) I don't have any C:\Qt\4.6.2 folder instead there is a configure.exe file in the folder C:\QtSDK\Desktop\Qt\4.8.0\mingw . Is it the same as C:\Qt\4.6.2 ?
If you were following the instruction you would have that folder... but you don't need it. With the Qt SDK you should start the Updater and download the Qt source code for the version you are using (e.g. 4.7.4). The source directory will then be something like "C:\QtSDK\QtSources\4.7.4". You do not need to do the Qt build... a built version matching the source is already in C:\QtSDK\Desktop\Qt\4.7.4\mingw (i.e. where %QTDIR is pointing). After you have completed the steps for creating libmysql.a you should be able to this in the "Qt command prompt":


cd C:\QtSDK\QtSources\4.7.4\src\plugins\sqldrivers\my sql
qmake "INCLUDEPATH+=C:\Program Files\MySQL\MySQL Server 5.5\include" "LIBS+=C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib" mysql.pro
mingw32-make
mingw32-make install

Anas
4th April 2012, 02:51
Thank you very much for guiding me in such a detailed way. Here are the screenshots of exact what commands I have run and what errors are being produced.

7557
7558
Some important points that I want to mention here are:
1) I tried using mingw-utils-0.4-1 but reimp command produced an error like "Invalid or corrupt library file" for libmysql.lib. So, I used mingw-utils-0.3 in the above screenshots. My mingw version is 3.15.2.
2) where will this libmysql.a file be created? Although reimp and dlltool commands run without producing any error or other output as shown in the above screenshots, I can at least NOT find a file named libmysql.a in that \MySQL Server5.5llib\ folder where libmysql.lib file is located.
3) and finally the errors produced by running qmake and mingw32-make are where I absolutely don't understand what's happening. Hope you will continue to help me. Thanks!!

ChrisW67
4th April 2012, 04:41
First screen capture: The warning regarding backslashes can be suppressed by using forward slashes in the command line:


qmake "INCLUDEPATH+=C:/Program Files/MySQL/MySQL Server 5.5/include" "LIBS+=C:/Program Files/MySQL/MySQL Server 5.5/lib/libmysql.a" mysql.pro

note also that I corrected the name of the library file.

Second screen capture: The error regarding a missing "mysql.h" implies that the INCLUDEPATH given in the command above is incorrect. Have a look for mysql.h in "C:/Program Files/MySQL/MySQL Server 5.5/include". Perhaps it is in a subdirectory? There may also be issues with spaces in the paths, but I don't know that for sure.

1) Did you install MingW using the instructions (http://www.mingw.org/wiki/Getting_Started instructions) and then ...?
mingw-get install mingw32-utils
2) The libmysql.a file should be in "C:/Program Files/MySQL/MySQL Server 5.5/lib/" where you ran the command. Do you see a matching DEF file there also?
3) See above.