Qt MySQL Query - Unable to Bind Value

Finally, I set up my MySQL connection in C ++ Qt. However, when I try to bind the values, I get the following error:

QSqlError("2036", "QMYSQL3: Unable to bind value", "Using unsupported buffer type: 1701052421 (parameter: 1)") 

I have these files:

Engine.h:

 #ifndef ENGINE_H #define ENGINE_H #include "database/mysql.h" class engine { private: static mysql* _mysql; public: static void initialize(); static void destroy(); static mysql get_mysql(); }; #endif // ENGINE_H 

Engine.cpp:

 #include "engine.h" #include "entities/member_controller.h" #include <QDebug> mysql* engine::_mysql; void engine::initialize() { _mysql = new mysql(); member* mem = member_controller::get_member(1); qDebug() << "mem name = " << mem->getFirstName() << " " << mem->getSecondName(); delete mem; } void engine::destroy() { delete _mysql; } mysql engine::get_mysql() { return *_mysql; } 

mysql.h:

 #ifndef MYSQL_H #define MYSQL_H #include <QtSql> #include <QString> #include "mysql_result.h" class mysql { private: QSqlDatabase db; public: mysql(); ~mysql(); mysql_result create_result(QString query); QSqlError error(); QSqlQuery query_prepare(QString query1) { QSqlQuery query(this->db); query.prepare(query1); // this->query = query; return query; } }; #endif // MYSQL_H 

(query_prepare temp. temp. in the header file just for verification)

mysql.cpp

 #include "mysql.h" mysql::mysql() { this->db = QSqlDatabase::addDatabase("QMYSQL", "QMYSQL"); this->db.setHostName("localhost"); this->db.setUserName("root"); this->db.setPassword("Eequi4"); this->db.setDatabaseName("test"); this->db.open(); } mysql::~mysql() { } QSqlError mysql::error() { return this->db.lastError(); } 

member_controller.h:

 #ifndef MEMBER_CONTROLLER_H #define MEMBER_CONTROLLER_H #include <QString> #include "member.h" class member_controller { public: static member* get_member(unsigned int id); static member* get_member(QString email); }; #endif // MEMBER_CONTROLLER_H 

member_controller.cpp:

 #include "member_controller.h" #include "database/mysql_result.h" #include "engine.h" #include "database/mysql_result.h" #include <QtSql/QSqlQuery> member* member_controller::get_member(unsigned int id) { QSqlQuery result = engine::get_mysql().query_prepare("SELECT * FROM members WHERE member_id = :mem_id"); result.bindValue(":mem_id", id); if (result.exec() && result.first()) { return new member(id, result.value("first_name").toString(), result.value("second_name").toString(), result.value("screen_name").toString(), result.value("email").toString(), result.value("status").toString()); } else { qDebug() << engine::get_mysql().error() << "\n"; qDebug() << result.lastError() << "\n"; } return new member(0, "", "", "", "", ""); } 

Hope this is all the necessary code. I tried using the questionnaire except: mem_id, but no luck.

+6
source share
1 answer

I am not an expert in C ++ or Qt and I am not able to debug your code.

But just because of curiosity, I began to examine your code and found a suspicious line (second) in your code:

 mysql_result result = engine::get_mysql().create_result("SELECT * FROM members WHERE member_id = ?"); 

Since I am not an expert, and you did not provide any includes , I do not know what your engine namespace is, nor the return type of the get_mysql() function, nor create_result .

So, I guessed a little: get_mysql() possibly returned a QSqlDatabase object? is not it?

But this type does not support any create_result method! So I got stuck there.

Next thanks google I found your one more question made a week ago. I would like to add such important information to your post next time so that people can see your classes and functions, such as:

 mysql_result mysql::create_result(QString query) { return mysql_result(this->db.exec(query)); } 

and

 mysql_result::mysql_result(QSqlQuery query) { this->query = query; } 

Now I can understand what this second line of your code is trying to do.

I see a suspicious thing here return mysql_result(this->db.exec(query)); . This is similar to the names of the functions you are trying to execute and get the result from the mysql server.

But according to the algorithm that I see in your member_controller::get_member , it seems to me that you only prepare at the stage, but do not execute it. I see that the Qt Documentation is not clear enough (and again I am not an expert) because: Executes an SQL statement in the database and returns a QSqlQuery Object. And technically you can say that you have QSqlQuery as a result, and you can expect this result to be exactly the same as if you did:

 //mysql_result mysql::create_result(QString query) QSqlQuery mysql::query_prepare(QString query) { QSqlQuery query(this->db); query.prepare(query); this->query = query; return this->query; } 

But IMHO this is not so. In your case, this has already been done, so you cannot bind any parameter later. So I suggest you change your mysql_result mysql::create_result(QString query) or create another QSqlQuery mysql::query_prepare(QString query) function that makes more sense to me. and change your first lines of code to:

 member* member_controller::get_member(int id) { QSqlQuery query = engine::get_mysql().query_prepare("SELECT * FROM members WHERE member_id = ?"); query.addBindValue(value); if (query.exec() && query.first()) ... ... 

And in the very last paragraph, I don’t understand why you are trying to invent a shadow? If you already have mysql Qt classes that look very good to me, why are you creating your own class and methods with a single line to call the Qt method, for example:

 void mysql_result::add_parameter(QVariant value) { this->queryObject.addBindValue(value); } 

Sorry, but IMHO this is not a good idea and almost does not make sense.

UPDATE Having looked at the updated code, I see:

  result.bindValue(":mem_id", id); 

where result a QSqlQuery type, so you call the native bindValue method, where the second parameter is const QVariant & val according to the documentation. Therefore, I would change your call to:

  result.bindValue(":mem_id", QVariant(id)); 

or

  QVariant mem_id(id); result.bindValue(":mem_id", mem_id); 
+5
source

All Articles