I finally fixed my MySQL connection in C++ Qt. However, when I try to bind 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 body temp. in header file just to test)
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, "", "", "", "", "");
}
I hope this is all the code needed. I tried using questionmark except of :mem_id but no luck either.