Results 1 to 6 of 6

Thread: Problem Connecting with MySQL in a commandline application

  1. #1
    Join Date
    Mar 2012
    Location
    Pakistan
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem Connecting with MySQL in a commandline application

    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.

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QtSql/QSqlDatabase>
    3.  
    4. #include <iostream>
    5.  
    6. using namespace std;
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11. QSqlDatabase myDb = QSqlDatabase::addDatabase( "QMYSQL" );
    12.  
    13. myDb.setHostName( "localhost" );
    14. myDb.setUserName( "root" );
    15. myDb.setPassword( "*********" );
    16. myDb.setDatabaseName( "mytestdb" );
    17.  
    18. bool success = myDb.open();
    19.  
    20. if ( success ) cout << "Database connection established successfully" << endl;
    21. else cout << "Database connection failed" << endl;
    22.  
    23. myDb.close();
    24.  
    25. QSqlDatabase::removeDatabase( myDb.database().connectionName() );
    26.  
    27. return a.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

    On building this project, I get following, about 18 errors.
    Please tell me what am I doing wrong and where?
    Thanks!!!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem Connecting with MySQL in a commandline application

    Qt Code:
    1. QT += sql
    To copy to clipboard, switch view to plain text mode 
    missing from your PRO file would be my first guess.

  3. The following user says thank you to ChrisW67 for this useful post:

    Anas (4th April 2012)

  4. #3
    Join Date
    Mar 2012
    Location
    Pakistan
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem Connecting with MySQL in a commandline application

    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 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
    Last edited by Anas; 3rd April 2012 at 20:06.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem Connecting with MySQL in a commandline application

    Quote Originally Posted by Anas View Post
    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":
    Qt Code:
    1. cd C:\QtSDK\QtSources\4.7.4\src\plugins\sqldrivers\mysql
    2. 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
    3. mingw32-make
    4. mingw32-make install
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to ChrisW67 for this useful post:

    Anas (4th April 2012)

  7. #5
    Join Date
    Mar 2012
    Location
    Pakistan
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem Connecting with MySQL in a commandline application

    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.
    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!!
    Last edited by Anas; 4th April 2012 at 01:58.

  8. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem Connecting with MySQL in a commandline application

    First screen capture: The warning regarding backslashes can be suppressed by using forward slashes in the command line:
    Qt Code:
    1. 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
    To copy to clipboard, switch view to plain text mode 
    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 and then ...?
    Qt Code:
    1. mingw-get install mingw32-utils
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by ChrisW67; 4th April 2012 at 03:48.

Similar Threads

  1. Problem with connecting to MySql using SSL
    By moosa in forum Qt Programming
    Replies: 2
    Last Post: 25th July 2011, 01:49
  2. Replies: 2
    Last Post: 30th March 2011, 10:11
  3. Replies: 3
    Last Post: 9th February 2011, 20:30
  4. Replies: 2
    Last Post: 15th April 2010, 14:59
  5. Problem Wtih Connecting MySQL Database on Windows
    By dummystories in forum Installation and Deployment
    Replies: 25
    Last Post: 29th April 2009, 11:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.