PDA

View Full Version : Casting in a select sqlQuery



Kesy
25th November 2008, 14:22
Hi all

I use a QSqlQueryModel to fetch data from a database, but since the db is SQLite the field date is String.
I want to do a casting before put the data in the QSqlQueryModel to have the value of date in a QDate, so I can sort by this filed and have the right result.

Any ideas?

Thanks

spirit
25th November 2008, 14:26
you can get data from a model in QVariant object and then you can use QVariant::toDate method.

Kesy
25th November 2008, 15:07
Thanks for the quick answer.
Do I have to parse all the rows of the query and transform them in QDate or do it on the model?
This is the format of the field:
"25.11.2008 16:05:00"
I tried to convert the QVariant with this string into a QDateTime but it doesn't work. It gets an empty QDateTime.

Thanks

spirit
25th November 2008, 15:12
the date/time/datetime format must be ISO


QDate QVariant::toDate () const
Returns the variant as a QDate if the variant has type() Date, DateTime, or String; otherwise returns an invalid date.
If the type() is String, an invalid date will be returned if the string cannot be parsed as a Qt::ISODate format date.
See also canConvert() and convert().

another way, if you don't what to change format in databse, you can use static method of QDate -- QDate::fromString and pass needed mask as the second parametr.