Results 1 to 8 of 8

Thread: SQL result problem

  1. #1
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default SQL result problem

    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:

    Qt Code:
    1. QModelIndexList selectedList = ui->tableView_clients->selectionModel()->selectedRows();
    2.  
    3.  
    4.  
    5. int selected_row;
    6. for( int i=0; i<selectedList.count(); i++)
    7. //QMessageBox::information(this,"", QString::number(selectedList.at(i).row()));
    8. selected_row=selectedList.at(i).row();
    9.  
    10. mod.setQuery("SELECT * FROM clients");
    11. int ClientId = mod.data(mod.index(selected_row, 1)).toInt();
    12.  
    13.  
    14. emit selectedRaw(QString::number(ClientId));
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    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: SQL result problem

    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.

  3. #3
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: SQL result problem

    Thanks for the replay.

    Qt Code:
    1. int ClientId = mod.data(mod.index(selected_row, 1));
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    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: SQL result problem

    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.

  5. #5
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: SQL result problem

    Thanks.Now it works!It was the wrong column.

  6. #6
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: SQL result problem

    Never deleted using this code:

    Qt Code:
    1. QModelIndexList selectedList = ui->tableView_clients->selectionModel()->selectedRows();
    2.  
    3. int selected_row;
    4. int i=0;
    5. selected_row=((selectedList.at(i).row())-1);
    6. QString row=QString::number(selected_row);
    7.  
    8. QSqlQuery query;
    9. query.prepare("DELETE from clients where ClientId="+row);
    10.  
    11. query.exec();
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL result problem

    Try this :
    Qt Code:
    1. QModelIndexList selectedList = ui->tableView_clients->selectionModel()->selectedRows();
    2.  
    3. int selected_row;
    4. int i=0;
    5. selected_row=((selectedList.at(i).row())-1);
    6. QString row=QString::number(selected_row);
    7.  
    8. QSqlQuery query;
    9. QString my_query = QString("DELETE from clients where ClientId=")+row;
    10. query.prepare(my_query);
    11.  
    12. query.exec();
    To copy to clipboard, switch view to plain text mode 

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

  8. #8
    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: SQL result problem

    ... 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?

Similar Threads

  1. Replies: 2
    Last Post: 13th December 2011, 08:44
  2. How i can plot result in my GUI ?
    By 21did21 in forum Qwt
    Replies: 58
    Last Post: 18th June 2011, 00:41
  3. QRegExp - get only last result
    By kabanek in forum Newbie
    Replies: 2
    Last Post: 3rd November 2010, 22:17
  4. Replies: 4
    Last Post: 17th September 2010, 20:18
  5. How to display result
    By sksingh73 in forum Newbie
    Replies: 1
    Last Post: 7th June 2010, 08:39

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.