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. [SOLVED] QSqlQuery bindValue can't convert to QVariant error.

[SOLVED] QSqlQuery bindValue can't convert to QVariant error.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 7.8k 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.
  • A Offline
    A Offline
    Anticross
    wrote on last edited by
    #1

    I try to connect to db and insert some values in table with the help of QSqlQuery:
    @
    QHashIterator <int, Item *> iter(m_events);
    QSqlQuery query(m_db);

    while(iter.hasNext()) {

        QString sql = QString("INSERT INTO %1 (AlarmID, EQTypeID, AlarmMsgTemplate, AlarmLevelESMA)" 
            "VALUES (:AlarmID, :EQTypeID, :AlarmMsgTemplate, :AlarmLevelESMA)").arg(TBL_EquipmentType);
    
        query.prepare(sql);
        query.bindValue(":AlarmID",          iter.value()->id);
        query.bindValue(":EQTypeID",         2);
        query.bindValue(":AlarmMsgTemplate", iter.value()->displayname);
        query.bindValue(":AlarmLevelESMA",   0);
        query.exec&#40;&#41;;
    
        iter.next(&#41;;
    }    @
    

    and get 4 errors:
    2>.\EventInjector.cpp(51) : error C2664: 'void QSqlQuery::bindValue(const QString &,const QVariant &,QSql::ParamType)' : cannot convert parameter 2 from 'int' to 'const QVariant &'
    2> Reason: cannot convert from 'int' to 'const QVariant'
    2> Source or target has incomplete type
    2>.\EventInjector.cpp(52) : error C2664: 'void QSqlQuery::bindValue(const QString &,const QVariant &,QSql::ParamType)' : cannot convert parameter 2 from 'int' to 'const QVariant &'
    2> Reason: cannot convert from 'int' to 'const QVariant'
    2> Source or target has incomplete type
    2>.\EventInjector.cpp(53) : error C2664: 'void QSqlQuery::bindValue(const QString &,const QVariant &,QSql::ParamType)' : cannot convert parameter 2 from 'QString' to 'const QVariant &'
    2> Reason: cannot convert from 'QString' to 'const QVariant'
    2> Source or target has incomplete type
    2>.\EventInjector.cpp(54) : error C2664: 'void QSqlQuery::bindValue(const QString &,const QVariant &,QSql::ParamType)' : cannot convert parameter 2 from 'int' to 'const QVariant &'
    2> Reason: cannot convert from 'int' to 'const QVariant'
    2> Source or target has incomplete type
    2>Build log was saved at "file://D:\Projects\sxSMA\trunk\sxSoftware\sxProject\Debug\BuildLog.htm"
    2>sxEventInjector - 4 error(s), 0 warning(s)
    In docs I found this example:

    Positional binding using named placeholders:
    @ QSqlQuery query;
    query.prepare("INSERT INTO person (id, forename, surname) "
    "VALUES (:id, :forename, :surname)");
    query.bindValue(0, 1001);
    query.bindValue(1, "Bart");
    query.bindValue(2, "Simpson");
    query.exec();
    @
    Binding values using positional placeholders (version 1):
    @
    QSqlQuery query;
    query.prepare("INSERT INTO person (id, forename, surname) "
    "VALUES (?, ?, ?)");
    query.bindValue(0, 1001);
    query.bindValue(1, "Bart");
    query.bindValue(2, "Simpson");
    query.exec();@

    Binding values using positional placeholders (version 2):
    @QSqlQuery query;
    query.prepare("INSERT INTO person (id, forename, surname) "
    "VALUES (?, ?, ?)");
    query.addBindValue(1001);
    query.addBindValue("Bart");
    query.addBindValue("Simpson");
    query.exec();@

    Binding values to a stored procedure:

    This code calls a stored procedure called AsciiToInt(), passing it a character through its in parameter, and taking its result in the out parameter.
    @QSqlQuery query;
    query.prepare("CALL AsciiToInt(?, ?)");
    query.bindValue(0, "A");
    query.bindValue(1, 0, QSql::Out);
    query.exec();
    int i = query.boundValue(1).toInt(); // i is 65@

    What I'm doing wrong ?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Anticross
      wrote on last edited by
      #2

      I found whats the problem: I include <QSqlQuery>,<QHash> and <QSqlDatabase>, but forget to include <QVariant>. After including problems gone. :)

      1 Reply Last reply
      0
      • U Offline
        U Offline
        unai_i
        wrote on last edited by
        #3

        Hello,
        As your compiler says "Source or target has incomplete type" I would check if QVariant or QString is included. (My guess: It may be QVariant as the same problem appears with int)

        1 Reply Last reply
        0
        • U Offline
          U Offline
          unai_i
          wrote on last edited by
          #4

          Ok I'm a little late on this.

          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