Results 1 to 7 of 7

Thread: MySQL connection problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 10:33. Reason: Missing tags

Similar Threads

  1. qt4.3.1 mysql problem
    By twells55555 in forum Qt Programming
    Replies: 5
    Last Post: 8th October 2007, 22: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, 18:59
  3. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 10:52
  4. MySQL starting problem
    By shamik in forum General Discussion
    Replies: 5
    Last Post: 25th April 2007, 08:20
  5. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 13: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
  •  
Qt is a trademark of The Qt Company.