Results 1 to 7 of 7

Thread: MySQL connection problem

  1. #1
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    1

    Default MySQL connection problem

    Hi,
    I'm using MySQL 5 and Qt 4.3 under Kubuntu 7.04.

    I am able to open a connection to the database OK if I do the following:

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    2. db.setDatabaseName("location");
    3. db.setUserName("root");
    4. db.setPassword("blah");
    5.  
    6. if (!db.open()) {
    7. cerr << "Database did not open successfully!" << endl;
    8. return false;
    9. }
    To copy to clipboard, switch view to plain text mode 

    However when I try and specify a unique connection name like the following, I am unable to connect:

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "someConnection");
    2. db.setDatabaseName("location");
    3. db.setUserName("root");
    4. db.setPassword("blah");
    5.  
    6. if (!db.open()) {
    7. cerr << "Database did not open successfully!" << endl;
    8. return false;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Does anyone know why I am unsuccessful in connecting in the second case?

    Additional information:
    In the second case the message "Database did not open successfully!" does not print. However when the first query is made the following error messages are received:

    QSqlQuery::exec: database not open
    Driver not loaded Driver not loaded

    A full listing of the erroneous code is shown below:

    Qt Code:
    1. #include <QApplication>
    2. #include <QMessageBox>
    3. #include <QSqlDatabase>
    4. #include <QSqlError>
    5. #include <QSqlQuery>
    6. //#include <QTextStream>
    7. #include <iostream>
    8. #include <QVariant>
    9. using namespace std;
    10.  
    11. static bool createConnection()
    12. {
    13. QString title;
    14.  
    15. QSqlQuery query;
    16. QString sql_string;
    17.  
    18. sql_string = "SELECT friend FROM friends WHERE username = \"Bob\"";
    19.  
    20. query.exec(sql_string);
    21.  
    22. if (!query.isActive())
    23. std::cerr << qPrintable(query.lastError().text()) << std::endl;
    24.  
    25.  
    26. while (query.next()) {
    27. title = query.value(0).toString();
    28. std::cerr << qPrintable(title) << endl;
    29. }
    30.  
    31. return true;
    32. }
    33.  
    34. int main()
    35. {
    36. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "SomeName");
    37. db.setDatabaseName("location");
    38. db.setUserName("root");
    39. db.setPassword("blah");
    40. if (!db.open()) {
    41. cerr << "Database did not open successfully!" << endl;
    42. return false;
    43. }
    44.  
    45.  
    46.  
    47. if (!createConnection())
    48. return 1;
    49.  
    50. return 0;
    51. }
    To copy to clipboard, switch view to plain text mode 


    Thanks,
    Myils
    Last edited by wysota; 9th October 2007 at 09:33. Reason: Missing tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MySQL connection problem

    Check if you have MySQL Qt driver compiled and if you have MySQL client library installed on your system (it's called libmysqlclient).

  3. #3
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    1

    Default Re: MySQL connection problem

    Hi wysota,
    I installed Qt4 from the Ubuntu software repository using apt-get. I'm guessing that the MySQL Qt driver is OK since I am able to successfully connect to MySQL provided I do not specify a value for connectionName when calling QSqlDatabase::addDatabase().

    I'm also guessing that the MySQL client library is installed on my system since I can find the following files:

    /var/lib/dpkg/info/libmysqlclient15off.shlibs
    /var/lib/dpkg/info/libmysqlclient15off.list
    /var/lib/dpkg/info/libmysqlclient15off.postinst
    /var/lib/dpkg/info/libmysqlclient15off.postrm
    /var/lib/dpkg/info/libmysqlclient15off.md5sums
    /var/lib/dpkg/info/libmysqlclient15-dev.list
    /var/lib/dpkg/info/libmysqlclient15-dev.postrm
    /usr/lib/libmysqlclient.so.15.0.0
    /usr/lib/libmysqlclient_r.so.15.0.0
    /usr/lib/libmysqlclient.so.15
    /usr/lib/libmysqlclient_r.so.15
    /usr/share/doc/libmysqlclient15off
    /usr/share/doc/libmysqlclient15off/EXCEPTIONS-CLIENT.gz
    /usr/share/doc/libmysqlclient15off/changelog.Debian.gz
    /usr/share/doc/libmysqlclient15off/README.Debian
    /usr/share/doc/libmysqlclient15off/copyright
    /usr/share/doc/libmysqlclient15off/NEWS.Debian.gz

    Thanks,
    Myils

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MySQL connection problem

    Maybe the location or user/password are invalid? Can you connect to the database passing the same data using mysql console (mysql -u root -p location)?

  5. #5
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    1

    Default Re: MySQL connection problem

    The "location" table and user/password are valid. I know this because I can connect provided I execute the following:
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

    However I cannot connect to MySQL when I do this:
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "someConnection");

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: MySQL connection problem

    Your query is surely invalid. You should associate it with the connection you created.
    Qt Code:
    1. QSqlQuery query(QSqlDatabase::database("SomeName"));
    2. query.exec("...");
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to wysota for this useful post:

    myils (9th October 2007)

  8. #7
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    1

    Default Re: MySQL connection problem

    Thanks wysota,
    Your last suggestion has fixed the problem .
    Myils

Similar Threads

  1. qt4.3.1 mysql problem
    By twells55555 in forum Qt Programming
    Replies: 5
    Last Post: 8th October 2007, 21:26
  2. Mysterious Qt4.2.0 Mysql 5.0.41 connection problem
    By locus in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2007, 17:59
  3. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  4. MySQL starting problem
    By shamik in forum General Discussion
    Replies: 5
    Last Post: 25th April 2007, 07:20
  5. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48

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.