[QSqlQuery] Requête non active
Bonjour,
Je vous sollicite aujourd'hui car j'ai un problème dans une de mes classe, j'essaye de faire des requêtes préparées grâce au module QtSql.
Seulement voila, mes requêtes sont tout inactive quand je les testes du coup impossible de récupérer les données de retour.
Voici ma classe:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#include <SVSPacket/SVSPacketAction.h>
SVSPacketAction::SVSPacketAction(const QSqlDatabase &db) : m_query(db)
{
this->m_log = new SVSLog();
}
SVSPacketAction::~SVSPacketAction()
{
delete this->m_log;
this->m_log = NULL;
}
const SVSPacket SVSPacketAction::action(const SVSPacket &packet)
{
SVSPacket p;
switch(packet.id())
{
case 0:
p = this->typeZero(packet);
break;
}
return p;
}
//SQL QUERY
void SVSPacketAction::prepareSQLQuery(const QString &query, const QVariantList &value)
{
this->m_query.prepare(query);
for(int i = 0 ; i < value.size() ; i++)
{
this->m_query.addBindValue(value.at(i));
}
}
bool SVSPacketAction::executeSQLQuery() const
{
QSqlQuery q = this->m_query;
if(q.execBatch())
{
this->m_log->show("SQL query has executed correctly.", SVSLog::Information);
this->m_log->show("Query [" + this->m_query.lastQuery() + "].", SVSLog::Information);
return true;
}
else
{
this->m_log->show("SQL query hasn't executed correctly : " + this->m_query.lastError().text() + ".", SVSLog::Error);
this->m_log->show("Query [" + this->m_query.lastQuery() + "].", SVSLog::Information);
return false;
}
}
const QVariantList SVSPacketAction::resultSQLQuery() const
{
QVariantList result;
if(this->m_query.isActive())
{
this->m_log->show("Query is active.", SVSLog::Information);
if(this->m_query.isSelect())
{
for(int i = 0 ; i < this->m_query.size() ; i++)
{
result.append(this->m_query.value(i));
}
}
}
else
{
this->m_log->show("Query is inactive.", SVSLog::Error);
}
return result;
}
//PACKET
const SVSPacket SVSPacketAction::typeZero(const SVSPacket &packet)
{
this->prepareSQLQuery("SELECT email from User WHERE email = ?", packet.data());
SVSPacket p;
if(this->executeSQLQuery())
{
p = SVSPacket(1);
QVariantList result = this->resultSQLQuery();
QString email = result.at(0).toString();
qDebug() << email;
}
else
{
p = SVSPacket(SVSPacket::Error);
}
return p;
} |