Qt-interest Archive, July 2007
Binding placeholder in QSqlQuery
Message 1 in thread
Hi everybody,
In the Qt documentation I find the following example for binding placeholders:
QSqlQuery query;
query.prepare("INSERT INTO person (id, forename, surname) "
"VALUES (:id, :forename, :surname)");
query.bindValue(":id", 1001);
query.bindValue(":forename", "Bart");
query.bindValue(":surname", "Simpson");
query.exec();
Using this exact example in my code, I get the following compilation error:
cougerimport.cpp|118| error: no matching function for call to
'QSqlQuery::bindValue(const char [4], int)'
/usr/include/QtSql/qsqlquery.h|88| note: candidates are: void
QSqlQuery::bindValue(const QString&, const QVariant&, QSql::ParamType)
/usr/include/QtSql/qsqlquery.h|89| note: void
QSqlQuery::bindValue(int, const QVariant&, QSql::ParamType)
cougerimport.cpp|119| error: no matching function for call to
'QSqlQuery::bindValue(const char [10], const char [5])'
/usr/include/QtSql/qsqlquery.h|88| note: candidates are: void
QSqlQuery::bindValue(const QString&, const QVariant&, QSql::ParamType)
/usr/include/QtSql/qsqlquery.h|89| note: void
QSqlQuery::bindValue(int, const QVariant&, QSql::ParamType)
cougerimport.cpp|120| error: no matching function for call to
'QSqlQuery::bindValue(const char [9], const char [8])'
/usr/include/QtSql/qsqlquery.h|88| note: candidates are: void
QSqlQuery::bindValue(const QString&, const QVariant&, QSql::ParamType)
/usr/include/QtSql/qsqlquery.h|89| note: void
QSqlQuery::bindValue(int, const QVariant&, QSql::ParamType)
What am I missing?
Thanks
Marius
--
[ signature omitted ]
Message 2 in thread
Hi, Marius.
Sounds like you've got QT_NO_ASCII_CAST defined.
Try to write the bindValue() statements like this:
query.bindValue( QString( ":id" ), 1001 );
Alternatively, use '?' as placeholders in the prepare() statement, and the bindValue( int, QVariant ) call to bind values to them.
Hope this helps.
Cheers
--
[ signature omitted ]
Message 3 in thread
On 7/16/07, Erik Haugen Bakke <erik_hb_mlist@xxxxxxxxxxxx> wrote:
> Hi, Marius.
Hi Erik,
Thanks for your reply.
>
> Sounds like you've got QT_NO_ASCII_CAST defined.
Is there a way to "undefine" it?
> query.bindValue( QString( ":id" ), 1001 );
It doesn't work, but :
query.bindValue(QString(":id"), QVariant(1001));
does work. It's a bit of a pain though.
>
> Alternatively, use '?' as placeholders in the prepare() statement, and the bindValue( int, QVariant ) call to bind values to them.
This option is not really convenient for the types of queries I'm
using. I would prefer not using this method. I would still have the
problem with the QVariant as well.
Thanks
Marius
--
[ signature omitted ]
Message 4 in thread
Marius Roets wrote:
> On 7/16/07, Erik Haugen Bakke <erik_hb_mlist@xxxxxxxxxxxx> wrote:
>> Hi, Marius.
> Hi Erik,
> Thanks for your reply.
>>
>> Sounds like you've got QT_NO_ASCII_CAST defined.
> Is there a way to "undefine" it?
Yes, but it's usually set for a reason. A better question is "do I need
to unset QT_NO_ASCII_CAST when it's there to prevent me from introducing
subtle bugs into my program and I don't really understand what it does
yet?" . These macros prevent implicit conversions between 8-bit byte
strings of unknown encoding and the Unicode string class QString. You
probably don't want to disable that protection unless you understand the
full consequences.
A simple `#undef QT_NO_ASCII_CAST' will not do what you want. The macro
actually modifies the behaviour of the Qt headers as they are included.
Undefining it won't have any effect after the Qt headers are included
(unless something like moc does horrid things with macros - and even
then it's not likely).
Note that QT_NO_ASCII_CAST and QT_NO_CAST_ASCII were renamed
QT_NO_CAST_TO_ASCII and QT_NO_CAST_FROM_ASCII in Qt4.
I strongly recommend reading:
http://doc.trolltech.com/qq/qq05-achtung.html
http://doc.trolltech.com/4.3/qstring.html#QT_NO_CAST_FROM_ASCII
... then deciding if you really want to unset QT_NO_ASCII_CAST (or Qt4
equivalent).
--
[ signature omitted ]