Results 1 to 9 of 9

Thread: Problem with MySQL

  1. #1
    Join Date
    Mar 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Problem with MySQL

    hi guys,

    I have problem with the connection to MySQL. I have created a database on localhost and iI connected to it, but the data doesn't show. I can see only empty lines like in attachment I use class QSqlRelationalTableModel.

    Here is my code:

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    2. db.setHostName("localhost");
    3. db.setDatabaseName("pokus");
    4. db.setUserName("root");
    5. db.setPassword("password");
    6. if (!db.open())
    7. {
    8. QMessageBox::warning(this, tr("Connection Error"), tr("Cannot open DB file"));
    9. return;
    10. }
    11.  
    12. m_mainModel = new QSqlRelationalTableModel(this);
    13. m_mainModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    14.  
    15. ui->tableView->setModel(m_mainModel);
    16.  
    17. m_mainModel->setTable("SPORTOVEC");
    18. if (!m_mainModel->select())
    19. {
    20. QMessageBox::warning(this, tr("Select Error"), m_mainModel->lastError().databaseText());
    21. return;
    22. }
    To copy to clipboard, switch view to plain text mode 

    sorry about my English.
    please help
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with MySQL

    It looks like you aren't specifying any filter or sort condition, so the "select()" statement is doing exactly what you've told it to do: select nothing.

    Look at the documentation for QSqlTableModel::select() and ::setFilter(). Try using setFilter( "*" ) and see what you get.

  3. #3
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: Problem with MySQL

    try ui->tableView->setModel(m_mainModel); after !m_mainModel->select()

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with MySQL

    Quote Originally Posted by unit View Post
    try ui->tableView->setModel(m_mainModel); after !m_mainModel->select()
    This doesn't matter, and it really is the wrong order to do things. If the model and table view are working together (as the Model/View archtecture says they should) then every time there is a new select(), the table view will automatically be updated because the model will emit a modelReset() or dataChanged() signal. It isn't necessary to tell the table about the model each time you do a select.

  5. #5
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: Problem with MySQL

    Quote Originally Posted by d_stranz View Post
    This doesn't matter, and it really is the wrong order to do things. If the model and table view are working together (as the Model/View archtecture says they should) then every time there is a new select(), the table view will automatically be updated because the model will emit a modelReset() or dataChanged() signal. It isn't necessary to tell the table about the model each time you do a select.
    Dear d_stranz. I'm not developer, but like read source code. And i known that you are good QT developer.
    But try my example and sali555

    Qt Code:
    1. if (!db.open())
    2. {
    3. QMessageBox::warning(this, tr("Connection Error"), tr("Cannot open DB"));
    4. return;
    5. }
    6.  
    7. m_mainModel = new QSqlRelationalTableModel(this, db);
    8. m_mainModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    9.  
    10. m_mainModel->setTable("SPORTOVEC");
    11. if (!m_mainModel->select())
    12. {
    13. QMessageBox::warning(this, tr("Select Error"), m_mainModel->lastError().databaseText());
    14. return;
    15. }
    16.  
    17. ui->tableView->setModel(m_mainModel);
    To copy to clipboard, switch view to plain text mode 

    It's work as sali555 want. Yes, ofcouse. We can use sort filter and other for prepare view column, and then use select statement, but is more easy way use select and then link model with view. In this way view automatically assign all column.

    So you should have not empty model to prepare your view.

    And sorry my English plz.
    Last edited by unit; 5th March 2011 at 18:51.
    Try read Qt documentation before ask stupid question.

  6. #6
    Join Date
    Mar 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with MySQL

    unit: you are right. it works perfect. thanks.

    Now I have a problem with a select query. I have a list of people( men and women) and now I would like to show only the rows with women. How can I do this?

    I know how I can do it with object QSqlQueryModel but I don't know how can I do this with QSqlRelationTableModel. Any Ideas?

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with MySQL

    You can either change your select statement to only select entries "where sex='F'" or you can add a QSortFilterProxyModel that will return false for "filterAcceptsRow() for any row where the sex is male. It is probably easier and more flexible just to change your select statement.

  8. #8
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: Problem with MySQL

    use setFilter ( const QString & filter )
    Sets the current filter to filter.
    The filter is a SQL WHERE clause without the keyword WHERE (for example, name='Josephine').
    If the model is already populated with data from a database, the model re-selects it with the new filter. Otherwise, the filter will be applied the next time select() is called.

    it's like m_mainModel->setFilter("sex=1"); or m_mainModel->setFilter("sex=\'F\'");
    Try read Qt documentation before ask stupid question.

  9. #9
    Join Date
    Mar 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with MySQL

    yeah, setFilter() works fine. thanks

Similar Threads

  1. Qt + MySQL problem
    By kiler69 in forum Installation and Deployment
    Replies: 3
    Last Post: 19th December 2010, 11:00
  2. MySql problem
    By MrShahi in forum Installation and Deployment
    Replies: 1
    Last Post: 25th May 2008, 00:18
  3. qt4.3.1 mysql problem
    By twells55555 in forum Qt Programming
    Replies: 5
    Last Post: 8th October 2007, 21:26
  4. MySQL starting problem
    By shamik in forum General Discussion
    Replies: 5
    Last Post: 25th April 2007, 07:20
  5. MySql Problem
    By vishesh in forum Installation and Deployment
    Replies: 7
    Last Post: 19th April 2007, 21:16

Tags for this Thread

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.