Results 1 to 4 of 4

Thread: Problem accessing to the database with a date[SOLVED]

  1. #1
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Problem accessing to the database with a date[SOLVED]

    Hi;

    In my application I have a QDateEdit and a database.

    I would like to send back from the database the users which date field are before the QDate in the QDateEdit.

    I tried the next code, but seems like QDate is not compatible (cannot convert to long,long) with the query:

    Qt Code:
    1. QDate testDate = ui.my_QDateEdit->date();
    2.  
    3. QSqlQuery _date((QString("SELECT id FROM users WHERE field_name_in_my_DB='%1' ")
    4. .arg(testDate), *myDatabase->GetDBPtr());
    To copy to clipboard, switch view to plain text mode 


    So I tried something that worked... but just when I need the same date, not before (lower), like I'd like:

    Qt Code:
    1. QString dday = QString::number(ui.my_QDateEdit->date().day());
    2. QString dmonth = QString::number(ui.my_QDateEdit->date().month());
    3. QString dyear = QString::number(ui.my_QDateEdits->date().year());
    4.  
    5. //In my database the "date" field is "year-month-day"
    6. QString strDate = dyear + "-" + dmonth + "-" + dday;
    7.  
    8. QSqlQuery _date((QString("SELECT id FROM users WHERE field_name_in_my_DB='%1' ")
    9. .arg(strDate), *myDatabase->GetDBPtr());
    To copy to clipboard, switch view to plain text mode 

    I don't know if it helps, but I'm using PostgreSQL.

    thanks!
    Last edited by jano_alex_es; 6th May 2009 at 11:16.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem accessing to the database with a date

    Hi, first of all use QSqlQuery::prepare() and QSqlQuery::bindValue() instead of arg(), and then what format has your database entry (timestamp, date or time)?

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

    jano_alex_es (6th May 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem accessing to the database with a date

    ... use QDateTime::toString ( const QString & format ) to get the right input format for your database.

  5. The following user says thank you to Lykurg for this useful post:

    jano_alex_es (6th May 2009)

  6. #4
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Problem accessing to the database with a date

    it works fine just including the prepare and binding instead of the quotation marks ^^

    thanks again!

Similar Threads

  1. QTable - Date Base Problem
    By esq in forum Qt Programming
    Replies: 4
    Last Post: 23rd May 2007, 02:32
  2. Replies: 7
    Last Post: 15th April 2007, 01:39
  3. problem in doing database program
    By wei243 in forum Qt Programming
    Replies: 0
    Last Post: 4th April 2007, 12:01
  4. Oracle Database Problem
    By magikalpnoi in forum Qt Programming
    Replies: 3
    Last Post: 27th September 2006, 21:19
  5. QProcess problem in accessing stdout
    By aruna in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2006, 17:56

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.