Results 1 to 5 of 5

Thread: Problem with qsqlquery's prepare and bindvalue

  1. #1
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with qsqlquery's prepare and bindvalue

    hi~
    I use mariadb database, and i compiled the qt plugins about mysql driver again. I want to use the QSqlQuery:repare() and bindValue() function to insert data to my table.but it doesn't work. The following is my code :
    QString strprepare = "INSERT INTO " + strIEDname + "_TREND ( q_max, q_ave, q_cnt, channelnum, datetime) "
    "VALUES ( :q_max, :q_ave,:q_cnt, :channelnum, :datetime)";
    ret_exec = query.prepare(strprepare);
    if (!ret_exec)
    {
    str_errmsg = query.lastError().text();
    }


    query.bindValue(":q_max", QVariant(iter->second.front().Q_max) );
    query.bindValue(":q_ave", QVariant(iter->second.front().Q_ave) );
    query.bindValue(":q_cnt", QVariant(iter->second.front().Q_cnt) );
    query.bindValue(":channelnum", QVariant(iter->second.front().channel) );
    query.bindValue(":datetime", QVariant(iter->second.front().datetime) );
    ret_exec = query.exec();
    if (!ret_exec)
    {
    str_errmsg = query.lastError().text();
    }

    function prepare() works well. but exec() works fail, and the error message is "Using unsupported buffer type: -842150451 (parameter: 1) QMYSQL3: Unable to bind value"。
    besides, i also use mysql 5.0 ,and it doesn't work either.

    I really need your help. Thank you!
    I 'm sorry for my poor english.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with qsqlquery's prepare and bindvalue

    What are the types of data you are pushing into QVariant instances?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qsqlquery's prepare and bindvalue

    here is the struct
    short channel;
    float Q_max;
    float Q_ave;
    float Q_cnt;
    QString datetime;


    Added after 5 minutes:


    if i use qsqlquery::exec(QString) directly, it works well. I think just cannot use the bindvalue function for mysql? besides, my qt version is 4.3.2.
    I also have another problem. it is that i want to insert binary data to my table. so i guess whether i must use prepare and bindvalue function to do that?
    thank you very much
    Last edited by yuzhouzhiwai; 5th November 2013 at 08:23.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with qsqlquery's prepare and bindvalue

    bindValue() for MySql works fine, I assure you.

    Try preparing a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2012
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qsqlquery's prepare and bindvalue

    That's really a problem.. I try this code below:
    //////////////////////////////////////////////////////////////
    QSqlDatabase db1 = QSqlDatabase::addDatabase("QMYSQL");
    db1.setHostName("localhost");
    db1.setDatabaseName("jfdb");
    db1.setUserName("root");
    db1.setPassword("root");
    bool ok = db1.open();
    if (!ok)
    {
    QMessageBox::warning(NULL, "è*¦å‘Š", db1.lastError().text() );
    return false;
    }
    bool r = db1.driver()->hasFeature(QSqlDriver::PreparedQueries);// return true
    QSqlQuery query(db1);
    QString str_errmsg, strIEDname = "IED0";
    QString strprepare = "INSERT INTO test ( id, num, datetime) "
    "VALUES (:id, :num, :datetime)";
    bool ret_exec = query.prepare(strprepare);// return true
    if (!ret_exec)
    {
    str_errmsg = query.lastError().text();
    }
    long l = 1;
    int n = 4;
    query.bindValue(":id", l );
    query.bindValue(":num", n );
    query.bindValue(":datetime", "2011-02-10 20:33:00" );
    ret_exec = query.exec();
    if (!ret_exec)// return false
    {
    str_errmsg = query.lastError().text();
    }


    It makes me feel very confused.

    my sql code for creating table :
    create table test(`id` bigint(10) auto_increment, `num` int(4), `datetime` datetime, primary key (`id`) );


    Added after 51 minutes:


    Maybe I have already solve the problem . The problem is the qt version. I need to study that.

    THank you very much~
    Last edited by yuzhouzhiwai; 5th November 2013 at 14:25.

Similar Threads

  1. Compilation problem with QSqlQuery::bindValue()
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 18th November 2013, 22:30
  2. Replies: 2
    Last Post: 18th March 2012, 23:28
  3. QSqlQuery::bindValue problem
    By segfault in forum Qt Programming
    Replies: 6
    Last Post: 11th March 2009, 07:27
  4. QSqlQuery prepare() bindValue() broken badly
    By RolandHughes in forum Qt Programming
    Replies: 4
    Last Post: 14th November 2008, 18:25
  5. Database and QSqlQuery::prepare problem
    By fengtian.we in forum Qt Programming
    Replies: 1
    Last Post: 31st October 2007, 23:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.