Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QSqlQuery bindValue()
Forum Updated to NodeBB v4.3 + New Features

Using QSqlQuery bindValue()

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 15.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joseph.s
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Also, I think that you should do:

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

        That is, include the colon in the binding code.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          joseph.s
          wrote on last edited by
          #4

          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".

          1 Reply Last reply
          0
          • A Offline
            A Offline
            arsinte_andrei
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              [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?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                arsinte_andrei
                wrote on last edited by
                #7

                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 )

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved