Results 1 to 1 of 1

Thread: Function to connect to a MySQL database

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Function to connect to a MySQL database

    Hello,

    I was doing some tests on alternative ways organize my Model/View approach, concerning access to a MySQL database.
    To create the model e used *tabelaPessoa().
    Then i tried to do the same with the connection, using *connection(QString newHostName, QString newDatabase, QString newUserName, QString newPassword).

    When i tried to connect i get the message:
    Qt Code:
    1. QSqlDatabasePrivate::database: unable to open database: "Access denied for user 'ODBC'@'localhost' (using password: NO) QMYSQL: Unable to connect"
    To copy to clipboard, switch view to plain text mode 

    Why is the message refering to ODBC .... ?

    This is the header file:
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5.  
    6. #include <QSqlTableModel>
    7. #include <QTableView>
    8. #include <QItemSelectionModel>
    9. #include <QMessageBox>
    10. #include <QDebug>
    11.  
    12. namespace Ui {
    13. class Dialog;
    14. }
    15.  
    16. class Dialog : public QDialog
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. explicit Dialog(QWidget *parent = 0);
    22. ~Dialog();
    23.  
    24. private slots:
    25. void on_connectDisconnect_clicked();
    26. void on_inserirUmRegisto_clicked();
    27. void on_eliminarRegisto_clicked();
    28. void on_atualizar_clicked();
    29.  
    30. private:
    31. Ui::Dialog *ui;
    32. QSqlTableModel *pessoa;
    33. QTableView *vista;
    34.  
    35. //Tabela Pessoa
    36. QSqlTableModel *tabelaPessoa()
    37. {
    38. model->setTable("pessoa");
    39. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    40. model->select();
    41. model->setHeaderData(0, Qt::Horizontal, "ID");
    42. model->setHeaderData(1, Qt::Horizontal, "Nome");
    43. model->setHeaderData(2, Qt::Horizontal, "Data Nasc.");
    44. model->setHeaderData(3, Qt::Horizontal, "Peso");
    45. model->setHeaderData(4, Qt::Horizontal, "Observações");
    46. return model;
    47. }
    48. QSqlDatabase *connection(QString newHostName, QString newDatabase, QString newUserName, QString newPassword)
    49. {
    50. db->addDatabase("QMYSQL");
    51. db->setHostName(newHostName);
    52. db->setDatabaseName(newDatabase);
    53. db->setUserName(newUserName);
    54. db->setPassword(newPassword);
    55. return db;
    56. }
    57. };
    58.  
    59. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    Ahhh ... i get it
    Qt Code:
    1. QSqlDatabase *db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));
    To copy to clipboard, switch view to plain text mode 
    Last edited by graciano; 14th December 2013 at 18:05.

  2. The following user says thank you to graciano for this useful post:


Similar Threads

  1. Cannot connect to mysql database
    By phapha in forum Newbie
    Replies: 3
    Last Post: 27th October 2011, 15:58
  2. Replies: 13
    Last Post: 17th June 2011, 18:19
  3. How to connect Qt with MYSQL??
    By Gokulnathvc in forum Newbie
    Replies: 10
    Last Post: 24th March 2011, 01:52
  4. Mysql unknown database, QMYSQL unable to connect
    By lixo1 in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2010, 22:39
  5. How to connect MySQL with QT
    By diego in forum Qt Programming
    Replies: 0
    Last Post: 27th May 2009, 06:34

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.