pnunn
26th February 2009, 00:17
Hi folks...
I'm trying to get an app working using parameterized queries that I have working using a hand worked query successfully.
The code is below... description of what's not happening follows.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
connect(ui->comboBox, SIGNAL(itemChanged(QModelIndex)), this, SLOT(showPriceData(QModelIndex)));
priceQueryModel = new QSqlQueryModel;
query = new QSqlQuery;
query->prepare("select p.shareid, p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid"
"where p.shareid = :share");
populateGroupBox();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updatePriceData(int idx)
{
QSqlRecord record = comboQueryModel->record(idx);
if(record.isEmpty())
{
qDebug() << "Error! Record is empty";
return;
}
QVariant shareidx = record.value("shareid");
query->bindValue(":share", shareidx.toInt());
qDebug() << query->lastQuery();
priceQueryModel->setQuery(*query);
if(priceQueryModel->lastError().isValid())
qDebug() << priceQueryModel->lastError();
ui->tableView->setModel(priceQueryModel);
priceQueryModel->removeColumn(0);
priceQueryModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Closing Date"));
priceQueryModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Share Price"));
priceQueryModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Total Value"));
priceQueryModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Company"));
}
void MainWindow::populateGroupBox(void)
{
comboQueryModel = new QSqlQueryModel;
comboQueryModel->setQuery("select sharename, shareid from shareid");
ui->comboBox->setModel(comboQueryModel);
}
The application output (from the query->lastQuery() call still has the variable as a variable, not as the bound value for some reason.
ie.
"select p.shareid, p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareidwhere p.shareid = :share"
Why is :share not the integer I've set it to?
Ta
Peter.
I'm trying to get an app working using parameterized queries that I have working using a hand worked query successfully.
The code is below... description of what's not happening follows.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
connect(ui->comboBox, SIGNAL(itemChanged(QModelIndex)), this, SLOT(showPriceData(QModelIndex)));
priceQueryModel = new QSqlQueryModel;
query = new QSqlQuery;
query->prepare("select p.shareid, p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid"
"where p.shareid = :share");
populateGroupBox();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updatePriceData(int idx)
{
QSqlRecord record = comboQueryModel->record(idx);
if(record.isEmpty())
{
qDebug() << "Error! Record is empty";
return;
}
QVariant shareidx = record.value("shareid");
query->bindValue(":share", shareidx.toInt());
qDebug() << query->lastQuery();
priceQueryModel->setQuery(*query);
if(priceQueryModel->lastError().isValid())
qDebug() << priceQueryModel->lastError();
ui->tableView->setModel(priceQueryModel);
priceQueryModel->removeColumn(0);
priceQueryModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Closing Date"));
priceQueryModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Share Price"));
priceQueryModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Total Value"));
priceQueryModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Company"));
}
void MainWindow::populateGroupBox(void)
{
comboQueryModel = new QSqlQueryModel;
comboQueryModel->setQuery("select sharename, shareid from shareid");
ui->comboBox->setModel(comboQueryModel);
}
The application output (from the query->lastQuery() call still has the variable as a variable, not as the bound value for some reason.
ie.
"select p.shareid, p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareidwhere p.shareid = :share"
Why is :share not the integer I've set it to?
Ta
Peter.