PDA

View Full Version : Qsqlquery fails to bindvalues



fantom
1st February 2012, 15:09
Dear all, please see below my code. The query fails to bind the values. Does anybody has an idea?


#include "simplechatserver.h"
#include <QBuffer>
#include <QByteArray>
#include <QString>
#include <QTcpSocket>
#include <QStringList>
#include <iostream>
#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlError>

SimpleChatServer::SimpleChatServer(QObject* parent) : QTcpServer(parent)
{
std::cout << "Message server started" << std::endl;
connect(this, SIGNAL(newConnection()), this, SLOT(addConnection()));
}

SimpleChatServer::~SimpleChatServer()
{
}

void SimpleChatServer::addConnection()
{
std::cout <<"new incomming connection" << std::endl;
QTcpSocket* connection = nextPendingConnection();
connections.append(connection);
QBuffer* buffer = new QBuffer(this);
buffer->open(QIODevice::ReadWrite);
buffers.insert(connection, buffer);
connect(connection, SIGNAL(disconnected()), SLOT(removeConnection()));
connect(connection, SIGNAL(readyRead()), SLOT(receiveMessage()));
}

void SimpleChatServer::removeConnection()
{
std::cout << "removing connection "<< std::endl;
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.take(socket);
buffer->close();
buffer->deleteLater();
connections.removeAll(socket);
socket->deleteLater();
}

void SimpleChatServer::receiveMessage()
{
std::cout << "receiving" <<std::endl;
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.value(socket);

// missing some checks for returns values for the sake of simplicity
qint64 bytes = buffer->write(socket->readAll());
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);

{
line = buffer->readLine();
QString str(line.data());
std::cout << str.toStdString() << std::endl;


QStringList list = str.split(QRegExp(" "), QString::SkipEmptyParts);



if (list.length() != 5) {
return;
}

// Extract device number
QString deviceNumber = list[0];
if (deviceNumber.isEmpty()) {
return;
} else
std::cout<< deviceNumber.toStdString() <<std::endl;
// Extract date
QString date = list[1];
if (date.isEmpty()) {
return;
}
// Extract time
QString timestamp = list[2];
if (timestamp.isEmpty()) {
return;
}
// Extract adherense
QString info = list[3];
if (info.isEmpty()) {
return;
}
// Extract cell_counter
QString counter = list[4];
if (counter.isEmpty()) {
return;
}



QSqlQuery query;
query.prepare("INSERT INTO Adherence VALUES ( :deviceID, :date, :time, :info, :counter)");
query.bindValue(":username", deviceNumber);
query.bindValue(":password", date);
query.bindValue(":firstname", timestamp);
query.bindValue(":lastname", info);
query.bindValue(":counter", counter);

if (query.exec() == false)
{
std::cout<< "Could not fill adherence table." <<std::endl;
return;
}



emit emitMessage(str);

}
}

Lykurg
1st February 2012, 15:25
Are you kidding us?
QSqlQuery query;
query.prepare("INSERT INTO Adherence VALUES ( :deviceID, :date, :time, :info, :counter)");
query.bindValue(":username", deviceNumber);
query.bindValue(":password", date);
query.bindValue(":firstname", timestamp);
query.bindValue(":lastname", info);
query.bindValue(":counter", counter);
If you can't see the error, I don't know how we should help you...

As for the million dollar question: Is a diviceID the same as an username?

fantom
1st February 2012, 15:36
I request humble apologies to the great minds.Names corrected.

Can you be so polite to tell me why i still can not bind the values? Do you have any great idea?

stampede
1st February 2012, 16:15
Can you be so polite to tell me why i still can not bind the values? Do you have any great idea?
Show the updated code. Arguments to "bindValue()" must exactly match the placeholder strings in "prepare()".

KillGabio
1st February 2012, 19:16
Lol, change your if for this one and tell us the error:


if (query.exec() == false)
{
qDebug () << query.lastError ().text ();
}