Joshua Bakker
Joshua Bakker

Reputation: 2378

QMYSQL query failing

I'm currently working on my project within C++ using Qt. I have MySQL as database storage and the idea is to make a small messenger like MSN and Skype.

However, my MySQL queries are failing. It simply gets no results or gives me an error. There's loads of code coming up, I'm sorry for that.

This is my mysql.cpp which creates and opens the database connection:

#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()
{
}

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

QSqlError mysql::error()
{
    return this->db.lastError();
}

The connection is opened. That works correctly.

This is my mysql_result.cpp, the file I use to add parameters, insert, get results etc:

#include "mysql_result.h"

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

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

void mysql_result::add_parameter(QString key, QVariant value)
{
    this->query.bindValue(key, value);
}

int mysql_result::get_last_id()
{
    return this->query.lastInsertId().toInt();
}

void mysql_result::execute()
{
    this->query.execBatch();
}

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

mysql_result::~mysql_result()
{
}

Okay, this should work. If I have the following code, it correctly returns all member first_name's from the database:

mysql_result res = _mysql->create_result("SELECT * FROM members");
QSqlQuery qry = res.get_query();
while (qry.next())
{
    qDebug() << qry.value("first_name");
}

In member_controller.cpp (the class I use to retrieve members by name/id), I got this:

member* member_controller::get_member(int id)
{
    mysql_result result = engine::get_mysql().create_result("SELECT * FROM members WHERE member_id = :ID");
    result.add_parameter(":ID", id);
    QSqlQuery query = result.get_query();

    if (query.exec() && query.next())
    {
        return new member(id, query.value("first_name").toString(), query.value("second_name").toString(), query.value("screen_name").toString(), query.value("email").toString(), query.value("status").toString());
    }
    else
    {
        qDebug() << engine::get_mysql().error() << "\n";
        qDebug() << query.lastError() << "\n";
    }

    return new member(0, "", "", "", "", "");
}

What it does it will go to the else, and I get the error invalid syntax near :ID. If I replace :ID with @ID (just like in C#), it will go to the else without error code.. I don't know what the problem is.

Two things. The code needs to be optimized a bit and made easier, I'm gonna work on that. Also, is it possible/allowed to put code in a pastebin and paste the URL rather than put the code here?

Upvotes: 0

Views: 1158

Answers (1)

user3791372
user3791372

Reputation: 4695

Try changing your query to this:

"SELECT * FROM members WHERE member_id = ?"

and add your param like this:

result.add_parameter(0, id);

I'd also suspect, if (query.exec() && query.next()) is incorrect, and the check for .next() should be removed as I'd imagine that requires another record to exist in the result set.

Upvotes: 1

Related Questions