View Full Version : Problem with qsqlquery's prepare and bindvalue

5th November 2013, 08:51
I use mariadb database, and i compiled the qt plugins about mysql driver again. I want to use the QSqlQuery::prepare() 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.

5th November 2013, 09:14
What are the types of data you are pushing into QVariant instances?

5th November 2013, 09:23
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

5th November 2013, 10:12
bindValue() for MySql works fine, I assure you.

Try preparing a minimal compilable example reproducing the problem.

5th November 2013, 15:25
That's really a problem.. I try this code below:
QSqlDatabase db1 = QSqlDatabase::addDatabase("QMYSQL");
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~