PDA

View Full Version : SQL result problem



unix7777
12th August 2012, 16:16
Hi,

i want to get value from selected table row (getting it's index) and get the record in first column from this record.
In accordance with documentation of QT it should be like this:


QModelIndexList selectedList = ui->tableView_clients->selectionModel()->selectedRows();

QSqlQueryModel mod;


int selected_row;
for( int i=0; i<selectedList.count(); i++)
//QMessageBox::information(this,"", QString::number(selectedList.at(i).row()));
selected_row=selectedList.at(i).row();

mod.setQuery("SELECT * FROM clients");
int ClientId = mod.data(mod.index(selected_row, 1)).toInt();


emit selectedRaw(QString::number(ClientId));


It doesn't display error but simply doesn't get the data i need.Instead every time i get 0.

ChrisW67
13th August 2012, 00:02
The row numbers from the model underlying the table view and the row number in the model created at line 11 are not necessarily related to each other. The query at line 11 can return the rows in any order. If the view sits on a table model based on "clients" then you can use that model to access the clientid.

It's also possible that column 1 (the second column) is not a numeric column, e.g. a varchar() column containing a string, that toInt() will always convert to 0.

The for() loop at line 7 ends at line 9, is that intentional? You will get only the last selected row at line 12 as it is now.

unix7777
13th August 2012, 08:03
Thanks for the replay.


int ClientId = mod.data(mod.index(selected_row, 1));

However i try to convert the value of ClientId (which is an integer in the DB) i get something like this:

../Faktura/clients.cpp:60: error: cannot convert 'QVariant' to 'int' in initialization

ChrisW67
13th August 2012, 08:31
QAbstractModel::data() returns QVariant and you are trying to stuff it in into an int. There's no suitable automatic conversion... hence the error message. That is what toInt() was for in your first code snippet.

You are getting zero constantly for one of these reasons:

The value in the second column of the selected_row of the model 'mod' is zero.
The value in the second column of the selected_row of model 'mod' is not a number and converts to zero.
There is no row selected_row in the model 'mod' so data() returns a null QVariant that converts to zero. This is entirely possible because the model underneath the view, and the model you are querying are unconnected.
The query "SELECT * FROM clients" is invalid, so model 'mod' contains no rows.

unix7777
13th August 2012, 08:56
Thanks.Now it works!It was the wrong column.

unix7777
13th August 2012, 11:26
Never deleted using this code:


QModelIndexList selectedList = ui->tableView_clients->selectionModel()->selectedRows();

int selected_row;
int i=0;
selected_row=((selectedList.at(i).row())-1);
QString row=QString::number(selected_row);

QSqlQuery query;
query.prepare("DELETE from clients where ClientId="+row);

query.exec();

Lesiok
13th August 2012, 13:36
Try this :
QModelIndexList selectedList = ui->tableView_clients->selectionModel()->selectedRows();

int selected_row;
int i=0;
selected_row=((selectedList.at(i).row())-1);
QString row=QString::number(selected_row);

QSqlQuery query;
QString my_query = QString("DELETE from clients where ClientId=")+row;
query.prepare(my_query);

query.exec();

Look what You get in my_query after line 9 and think why.

ChrisW67
13th August 2012, 23:31
... and be aware that the model row number and and the ClientID are not necessarily related to each other in any way.

Why don't you use the model to QAbstractItemModel::removeRow() the selected item?