PDA

View Full Version : Problem accessing to the database with a date[SOLVED]



jano_alex_es
6th May 2009, 10:29
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:



QDate testDate = ui.my_QDateEdit->date();

QSqlQuery _date((QString("SELECT id FROM users WHERE field_name_in_my_DB='%1' ")
.arg(testDate), *myDatabase->GetDBPtr());



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



QString dday = QString::number(ui.my_QDateEdit->date().day());
QString dmonth = QString::number(ui.my_QDateEdit->date().month());
QString dyear = QString::number(ui.my_QDateEdits->date().year());

//In my database the "date" field is "year-month-day"
QString strDate = dyear + "-" + dmonth + "-" + dday;

QSqlQuery _date((QString("SELECT id FROM users WHERE field_name_in_my_DB='%1' ")
.arg(strDate), *myDatabase->GetDBPtr());


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

thanks!

Lykurg
6th May 2009, 10:38
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)?

Lykurg
6th May 2009, 10:43
... use QDateTime::toString ( const QString & format ) to get the right input format for your database.

jano_alex_es
6th May 2009, 11:16
it works fine just including the prepare and binding instead of the quotation marks ^^

thanks again!