K
K
Konstantin2015-04-01 17:59:44
Qt
Konstantin, 2015-04-01 17:59:44

How to use QSqlQuery in your class constructor?

I create a player class:

#include "player.h"

Player::Player(int id = 0)
{
    query.prepare("SELECT * FROM player WHERE id=:id");
    query.bindValue(":id",id);
    query.exec();

}
QString Player::getName()
{
    return name;
}

Player::~Player()
{

}

Header file:
#ifndef PLAYER_H
#define PLAYER_H
#include <QSqlQuery>

class Player
{

public:
    Player(int id);
    ~Player();
    QString getName();
private:
    QString name;
    QSqlQuery query;

};

#endif // PLAYER_H

But when building, an error is written: no matching function for call to 'QSqlQuery::bindValue(const char [4], int&)'
query.bindValue(":id",id);
Tell me what's the matter, I'm completely confused, because in the main class such a sql query construction works ..
As I understand it, bindValue accepts a QVariant parameter, so it's all the same that I pass a number or a string there.
Apparently something messed up with the pointers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tugo, 2015-04-01
@happy_yar

Is there a directive in the player.cpp file #include <QVariant>?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question