Problem with QVariant, struct and SQLite



  • Hi,

    I have tried to search for an answer but didn't manage to get this work, and I don't know if this is even supposed to work. I am trying to insert my own struct as BLOB to SQLite database and the compile is ok but it is not working still.

    This is what I have tried:

    header:

    @struct serieinfo {
    int repeats_;
    double weights_;
    };

    Q_DECLARE_METATYPE(serieinfo)@

    source:

    @
    serieinfo temp1;
    temp1.repeats_ = 12;
    temp1.weights_ = 70;

    QVariant v;
    v.setValue(temp1);

    if (db.isOpen()) {

        QSqlQuery query;
    
        query.prepare(QString("INSERT INTO TEST VALUES(NULL,?,?,?,?,?,?,?)"));
    
        query.addBindValue("Bench Press");
        query.addBindValue(v);
        query.addBindValue(v);
        query.addBindValue(v);
        query.addBindValue(v);
        query.addBindValue(v);
        query.addBindValue(v);
    
        ret = query.exec();
    
        qDebug() << query.lastError();
    

    }@

    The SQLite table is created like this and it's working fine:

    @
    QSqlQuery query;
    ret = query.exec(QString("create table '%1'"
    "(id integer primary key, "
    "excercisename varchar(50), "
    "serie1 blob, "
    "serie2 blob, "
    "serie3 blob, "
    "serie4 blob, "
    "serie5 blob, "
    "serie6 blob)").arg(workoutname));
    @

    The outcome is that I have the table created but there is only "Bench Press". I am developing with Qt Creator on Windows 8. What am I doing wrong here? Thanks in advance!



  • Hi,

    I think your problem is that you try to bind a single value to multiple places in your query, which is not allowed.

    http://qt-project.org/doc/qt-5/qsqlquery.html#bindValue

    Tomas


Log in to reply
 

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