Results 1 to 7 of 7

Thread: QSqlQueryModel - some questions about using

  1. #1
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QSqlQueryModel - some questions about using

    Hello!

    I've got some simple questions about using QSqlQueryModel. When I do:

    Qt Code:
    1. queryModel->setQuery("SELECT * FROM my_table, bdb);
    To copy to clipboard, switch view to plain text mode 

    How can I know how many colums and rows I've got? How can I read one chosen row? How can I get names of each column? I've read class reference but I can't find that.

    thanks in advance
    best regards
    Tomasz

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: QSqlQueryModel - some questions about using

    Quote Originally Posted by Tomasz View Post

    Qt Code:
    1. queryModel->setQuery("SELECT * FROM my_table, bdb);
    To copy to clipboard, switch view to plain text mode 

    How can I know how many colums and rows I've got?
    for row:
    Qt Code:
    1. queryModel->rowCount();
    To copy to clipboard, switch view to plain text mode 
    for column:
    Qt Code:
    1. queryModel->columnCount();
    To copy to clipboard, switch view to plain text mode 

    How can I read one chosen row?
    Qt Code:
    1. QSqlRecord rec = queryModel->record(row_num);
    2. rec.value(col_num); // rec.value(coL_name);
    To copy to clipboard, switch view to plain text mode 

    How can I get names of each column?
    Qt Code:
    1. queryModel->headerData(col_num, Qt::Horizontal).toString();
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to saa7_go for this useful post:

    Tomasz (11th August 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQueryModel - some questions about using

    To obtain header data from model use QSqlQueryModel::headerData() function with appropriate orientation and default role.

    If you have no intention to display data in a view (e.g. QTableView wich automatically acquires data from model e.g. QSqlQueryModel) then QSqlQueryshould be used to manipulate data obtained from db. Anyway under-laying QSqlQuery object can be obtained from QSqlQueryModel by calling QSqlQueryModel::query() function.

    In my practice I never use '*' in query, I use column names. I have some problems with that approach in the past when I was changing and refactoring the code (I can not remember exact problems I have experienced).

  5. #4
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQueryModel - some questions about using

    @saa7_go thanks! It is very helpful! One more question. You've wrote:

    Qt Code:
    1. QSqlRecord rec = queryModel->record(row_num);
    2. rec.value(col_num); // rec.value(coL_name);
    To copy to clipboard, switch view to plain text mode 

    is there any simple way to read rows only with specified values of fields that i want. I mean something like "SELECT * FROM my_table WHERE name='something'"?

    thanks in advance
    best regards
    Tomasz
    Last edited by Tomasz; 11th August 2010 at 22:15.

  6. #5
    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: QSqlQueryModel - some questions about using

    You can obviously query to get the exact rows you want You can also use QAbstractItemModel::match() to find row or rows with matching values, or you can use QSortFilterProxyModel to show a subset of a model. If you use QSqlTableModel then the fiter() method may be of use. It depends on exactly what you want to achieve.

    A note of caution about using rowCount(): it's value is not reflective of the entire data set until all the rows have been fetched: see QAbstractItemModel::canFetchMore() and QAbstractItemModel::fetchMore().

  7. #6
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQueryModel - some questions about using

    Ok, I'll check QAbstractItemModel::match()
    But can You say something more about what You said?
    Quote Originally Posted by ChrisW67 View Post
    A note of caution about using rowCount(): it's value is not reflective of the entire data set until all the rows have been fetched: see QAbstractItemModel::canFetchMore() and QAbstractItemModel::fetchMore().
    How should I do it?

    thanks in advance
    best regards
    Tomasz

  8. #7
    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: QSqlQueryModel - some questions about using

    If your table or query returns many rows (>255 I think) then the QSqlTableModel (and the underlyin QSql*) fetches the first block of rows only and delays fetching more rows until you request them. At this point the rowCount() might return 255 or the total number of rows in the table depending on your database capabilities. To be certain you have the actual number of rows returned by your query you may need to force retrieval of the rows:
    Qt Code:
    1. while (model->canFetchMore()) model->fetchMore();
    To copy to clipboard, switch view to plain text mode 
    Sqlite requires this.

Similar Threads

  1. [Qt] TableView and QSqlQueryModel
    By Xandareva in forum Newbie
    Replies: 5
    Last Post: 7th April 2010, 14:20
  2. Does 'QSqlQueryModel' have a bug?
    By nuntawat in forum Qt Programming
    Replies: 8
    Last Post: 6th April 2010, 17:45
  3. About QSqlQueryModel
    By vinny gracindo in forum Newbie
    Replies: 1
    Last Post: 11th December 2009, 22:27
  4. how can I refresh QSqlQueryModel?
    By hashb in forum Qt Programming
    Replies: 3
    Last Post: 20th June 2009, 03:39
  5. QSqlTableModel/QSqlQueryModel
    By dragon in forum Newbie
    Replies: 11
    Last Post: 14th December 2006, 15:37

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
  •  
Qt is a trademark of The Qt Company.