PDA

View Full Version : QSqlQueryModel - some questions about using



Tomasz
11th August 2010, 11:58
Hello!

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



queryModel->setQuery("SELECT * FROM my_table, bdb);


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

saa7_go
11th August 2010, 13:04
queryModel->setQuery("SELECT * FROM my_table, bdb);


How can I know how many colums and rows I've got?

for row:

queryModel->rowCount();
for column:

queryModel->columnCount();



How can I read one chosen row?




QSqlRecord rec = queryModel->record(row_num);
rec.value(col_num); // rec.value(coL_name);



How can I get names of each column?

queryModel->headerData(col_num, Qt::Horizontal).toString();

pervlad
11th August 2010, 13:33
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 QSqlQuery should 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).

Tomasz
11th August 2010, 21:38
@saa7_go thanks! It is very helpful! One more question. You've wrote:



QSqlRecord rec = queryModel->record(row_num);
rec.value(col_num); // rec.value(coL_name);


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

ChrisW67
11th August 2010, 23:03
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().

Tomasz
11th August 2010, 23:14
Ok, I'll check QAbstractItemModel::match()

But can You say something more about what You said?

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

ChrisW67
12th August 2010, 23:43
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:


while (model->canFetchMore()) model->fetchMore();

Sqlite requires this.