Results 1 to 4 of 4

Thread: Casting in a select sqlQuery

  1. #1
    Join Date
    Oct 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Casting in a select sqlQuery

    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

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Casting in a select sqlQuery

    you can get data from a model in QVariant object and then you can use QVariant::toDate method.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Oct 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Casting in a select sqlQuery

    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

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Casting in a select sqlQuery

    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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.