Results 1 to 1 of 1

Thread: problem with Qt example

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default

    Qt sql example sql QueryModel needs something...fails right off!
    Qt Code:
    1. static bool createConnection()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. // db.setDatabaseName(":memory:");
    5. db.setDatabaseName("landonx");
    6. if (!db.open()) {
    7. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    8. qApp->tr("Unable to establish a database connection.\n"
    9. "This example needs SQLite support. Please read "
    10. "the Qt SQL driver documentation for information how "
    11. "to build it.\n\n"
    12. "Click Cancel to exit."), QMessageBox::Cancel);
    13. return false;
    14. }
    To copy to clipboard, switch view to plain text mode 

    The code I got out of Qt 4 docs works:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include <QMessageBox>
    4. #include <QSqlDatabase>
    5. #include <QSqlError>
    6. #include <QSqlQuery>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    12. db.setHostName("localhost");
    13. db.setDatabaseName("landonx");
    14. db.setUserName("landon4");
    15. db.setPassword("141");
    16. bool ok = db.open();
    17. QSqlDatabase defaultDB = QSqlDatabase::database();
    18.  
    19. QSqlQuery query;
    20. query.exec("SELECT * FROM log_book");
    21.  
    22. while (query.next()) {
    23. int id = query.value(0).toInt();
    24. QString fdate = query.value(1).toString();
    25. QString actype = query.value(2).toString();
    26. QString acid = query.value(3).toString();
    27. double nlandings = query.value(4).toDouble();
    28. double nhours = query.value(5).toDouble();
    29. qDebug() << id <<"\t"<< fdate<<"\t" << actype<<"\t" << acid<<"\t" << nlandings<<"\t" << nhours;
    30. }
    31. db.close();
    32.  
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 

    figured it out!

    Had to add:

    Qt Code:
    1. db.setHostName("localhost");
    2. db.setDatabaseName("landonx");
    3. db.setUserName("landon4");
    4. db.setPassword("Kateaux3141");
    To copy to clipboard, switch view to plain text mode 
    I see changing one "thing" doesn't fix all!
    Last edited by wysota; 3rd December 2009 at 10:46.

Similar Threads

  1. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  2. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  3. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  4. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.