[Qt-interest] QSqlQuery used with bindValue, returns QSqlError(-1, "", "") last error when query.exec() executes

Martin Hofius Martin at hofius-online.com
Fri Aug 21 14:42:03 CEST 2009


Hi,

Am Freitag, 21. August 2009 schrieb Sean Harmer:
> Hi,
>
> On Thursday 20 Aug 2009 20:28:30 S. Aguinaga wrote:
> > Thanks you for the feedback, I truly appreciate it.
...
> >
> > ok. So, I get the following when I use: qDebug() <<
> > query.lastError();"QSqlError(-1, "", "") "
> >
> > so, the line: QDate::currentDate().toString("dd,mm,yyyy") is not going to
> > be a QString? because if I type qDebug() <<
> > QDate::currentDate().toString("dd,mm,yyyy"); it sends the String to the
> > console.
>
> Yes, it will be a string but my point is that it is a string that contains
> commas which is what SQL uses as a delimiter for statements. So your date
> string will be interpreted by SQL as 3 columns of data not just one. This
the database should not interpret a bound value as part of a sql statement. 
Nevertheless the value in the bind variable must be converted to the internal 
database field format - and I doubt if Qt or the database recognises a 
format "dd,mm,yyyy")...
So you should use a common sql date format like 'yyyy-mm-dd' (which can also 
be used in a sql statement itself - not recommended because of performance 
and security issues) or (even better) provide the value as an variant (as 
described by sean below).

> will lead to a mismatch between the number of columns you specify and the
> number of columns of data that your appear to provide in the query.
no, see above
>
> Looking at the QSqlQuery::bindValue() docs, it seems that you do not need
> to explicitly convert your QDate object to a string. Just wrap it in a
> QVariant and let Qt do the work for you. i.e. Just try:
>
> QVariant v( QDate::currentDate() );
> query.bindValue( ":CreateDate", v );
>
> Qt should then take care of converting this to a form that your database
> can understand. If that does not work, then you need to find out what
> format your particular DB expects to be given a date in.
I guess 'yyyy-mm-dd' ... (common date format for sql databases)

Greetings
Martin



More information about the Qt-interest-old mailing list