PDA

View Full Version : Problem with MySQL



sali555
4th March 2011, 15:09
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:


QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("pokus");
db.setUserName("root");
db.setPassword("password");
if (!db.open())
{
QMessageBox::warning(this, tr("Connection Error"), tr("Cannot open DB file"));
return;
}

m_mainModel = new QSqlRelationalTableModel(this);
m_mainModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

ui->tableView->setModel(m_mainModel);

m_mainModel->setTable("SPORTOVEC");
if (!m_mainModel->select())
{
QMessageBox::warning(this, tr("Select Error"), m_mainModel->lastError().databaseText());
return;
}

sorry about my English.
please help

d_stranz
5th March 2011, 17:08
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.

unit
5th March 2011, 17:20
try ui->tableView->setModel(m_mainModel); after !m_mainModel->select()

d_stranz
5th March 2011, 18:10
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.

unit
5th March 2011, 18:34
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




if (!db.open())
{
QMessageBox::warning(this, tr("Connection Error"), tr("Cannot open DB"));
return;
}

m_mainModel = new QSqlRelationalTableModel(this, db);
m_mainModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

m_mainModel->setTable("SPORTOVEC");
if (!m_mainModel->select())
{
QMessageBox::warning(this, tr("Select Error"), m_mainModel->lastError().databaseText());
return;
}

ui->tableView->setModel(m_mainModel);

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.

sali555
5th March 2011, 21:33
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?

d_stranz
5th March 2011, 23:16
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.

unit
6th March 2011, 07:29
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\'");

sali555
6th March 2011, 15:34
yeah, setFilter() works fine. thanks