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



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



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



  • 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)



  • Ok I'm a little late on this.


Log in to reply
 

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