Results 1 to 3 of 3

Thread: connect mysql with odbc

  1. #1
    Join Date
    Apr 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default connect mysql with odbc

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "QtSql"
    4. #include "QLabel"
    5. #include "QMessageBox"
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void MainWindow::on_pushButton_clicked()
    20. {
    21. n->addDatabase("QODBC3","ahm");
    22. n->setHostName("10.0.0.101");
    23. n->setDatabaseName("Driver={MySQL ODBC 5.1.8 Driver};DATABASE=ahm;");
    24. n->setUserName("ahm");
    25. n->setPassword("ahmdtcahm");
    26. n->setPort(3306);
    27.  
    28. /*if (!n->open()) {
    29.  
    30.   QMessageBox::critical(0, QObject::tr("Database Error"),
    31.  
    32.   n->lastError().text());
    33.   }*/
    34. bool cek=n->open();
    35. if(cek==true)
    36. {
    37. QLabel *l=new QLabel();
    38. l->setText("connect");
    39. l->show();
    40. }
    41. else
    42. {
    43. QLabel *l=new QLabel();
    44. l->setText("can't connect");
    45. l->show();
    46.  
    47. }
    48. }
    To copy to clipboard, switch view to plain text mode 

    and output is can't connect...how can i solve this problem???
    Last edited by high_flyer; 18th April 2011 at 09:37. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: connect mysql with odbc

    Are ODBC correctly configured? What is the Error Message?
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    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: connect mysql with odbc

    As written your code won't even compile. At line 22: you try to treat a pointer to an object as an actual object. At line 23: QSqlDatabase::addDatabase() is a static method and you need to use it correctly (as in the examples in the documentation or this site). Try the SQL Widget Mapper Example.

Similar Threads

  1. How to connect Qt with MYSQL??
    By Gokulnathvc in forum Newbie
    Replies: 10
    Last Post: 24th March 2011, 00:52
  2. source code for odbc driver or MySql driver in arm-embedded-linux
    By sattu in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 24th January 2011, 10:11
  3. Qt connect different MySQL
    By weixj2003ld in forum Qt Programming
    Replies: 0
    Last Post: 5th August 2009, 08:35
  4. How to connect MySQL with QT
    By diego in forum Qt Programming
    Replies: 0
    Last Post: 27th May 2009, 05:34
  5. ODBC Oracle. Unable to connect to database
    By egil in forum Qt Programming
    Replies: 2
    Last Post: 17th October 2008, 13:46

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.