Using QSqlQuery bindValue()



  • Hi,

    I've just started using Qt 4.6. I have the following code:

    @User *UserMapper::getByUsername(QString username)
    {
    // Get user from database.
    QString sql = "select username, password from users "
    "where username = :username";
    QSqlQuery query;
    if (false == query.prepare(sql)) {
    qDebug("Failed to prepare query.");
    return 0;
    }

    query.bindValue("username", username);
    

    ...
    }
    @

    However, it does not compile:

    bq. error C2664: 'void QSqlQuery::bindValue(const QString &,const QVariant &,QSql::ParamType)' : cannot convert parameter 2 from 'QString' to 'const QVariant &'

    I must be missing something very obvious. Do I have to manually convert the QString to QVariant? How do I do that?

    Thanks!


  • Moderators

    Have you tried using "QVariant(username)" instead of using "username" directly? The conversion should actually happen implicitly... maybe you forgot to include the QVariant header?



  • Also, I think that you should do:

    @query.bindValue(":username", username);@

    That is, include the colon in the binding code.



  • Thank you very much for the help! You are both correct. I actually did forget to include the QVariant header, and it is supposed to be ":username" instead of "username".



  • Can you edit your first post Please in order to let other know that this problem was Solved. Just Add [SOLVED] in front of the main title



  • [quote author="arsinte_andrei" date="1408621013"]Can you edit your first post Please in order to let other know that this problem was Solved. Just Add [SOLVED] in front of the main title[/quote]

    Why do you kick the topic alive again after more than four years just to add that off-topic comment?



  • sorry it was my fault - I've been searching for something and I did not realise the date... I was ready to answer to this problem because Ihad even myself some trouble with bindValue when I've realised that it was already answered ( please note that I did not look at the date - but is a good lesson for me - it will never repeat again sorry )


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.