PDA

View Full Version : about QSqlQuery ,how can i get columns



tsuibin
1st March 2010, 09:53
QSqlQuery query;
query.exec("select * from table");



how to know
number of columns

i see the help

int QSqlQuery::size () const
Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. Note that for non-SELECT statements (isSelect() returns false), size() will return -1. If the query is not active (isActive() returns false), -1 is returned.


but it return -1, i use sqlite database

is there other ways to do

Lykurg
1st March 2010, 10:39
QSqlQuery::record() and QSqlRecord::count():
query->record()->count();(Assuming record is valid!)

waynew
2nd March 2010, 00:53
If you created the table, you already know the number of columns. What is it you are trying to do?

sparticus_37
28th March 2010, 20:29
to find the column id tto use QSqlQuery::value() then look here
QSqlQuery Reference (http://doc.trolltech.com/4.6/qsqlquery.html)
your looking for QSqlQuery::record()

now for finding the number of records return by you query you can do something like this



QSqlQuery q;
q.exec("SELECT Count(*) FROM tablename WHERE condition");
q.next();
int numRows = q.value(0).toInt();
//if you expect this query to return a massive number of records don use int for this purpose

//now run your original query


hope this helps