Results 1 to 6 of 6

Thread: Parameterized query not working.

  1. #1
    Join Date
    Dec 2008
    Location
    Upper Ferntree Gully, Victoria, Australia
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Parameterized query not working.

    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.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    6. {
    7. ui->setupUi(this);
    8. connect(ui->comboBox, SIGNAL(itemChanged(QModelIndex)), this, SLOT(showPriceData(QModelIndex)));
    9.  
    10. priceQueryModel = new QSqlQueryModel;
    11. query = new QSqlQuery;
    12. 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"
    13. "where p.shareid = :share");
    14.  
    15. populateGroupBox();
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void MainWindow::updatePriceData(int idx)
    24. {
    25. QSqlRecord record = comboQueryModel->record(idx);
    26. if(record.isEmpty())
    27. {
    28. qDebug() << "Error! Record is empty";
    29. return;
    30. }
    31. QVariant shareidx = record.value("shareid");
    32. query->bindValue(":share", shareidx.toInt());
    33. qDebug() << query->lastQuery();
    34.  
    35. priceQueryModel->setQuery(*query);
    36. if(priceQueryModel->lastError().isValid())
    37. qDebug() << priceQueryModel->lastError();
    38.  
    39. ui->tableView->setModel(priceQueryModel);
    40. priceQueryModel->removeColumn(0);
    41. priceQueryModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Closing Date"));
    42. priceQueryModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Share Price"));
    43. priceQueryModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Total Value"));
    44. priceQueryModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Company"));
    45. }
    46.  
    47. void MainWindow::populateGroupBox(void)
    48. {
    49. comboQueryModel = new QSqlQueryModel;
    50. comboQueryModel->setQuery("select sharename, shareid from shareid");
    51. ui->comboBox->setModel(comboQueryModel);
    52. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Parameterized query not working.

    I don't see any database connection. did you make it? show us compilable example.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Dec 2008
    Location
    Upper Ferntree Gully, Victoria, Australia
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Parameterized query not working.

    Sorry... the connection is in connection.h (based on qt examples)
    Qt Code:
    1. #ifndef CONNECTION_H
    2. #define CONNECTION_H
    3.  
    4. #include <QMessageBox>
    5. #include <QSqlDatabase>
    6. #include <QSqlError>
    7. #include <QSqlQuery>
    8.  
    9. /*
    10.   This file defines a helper function to open a connection to
    11.   the shares database (hard coded).
    12. */
    13.  
    14. static bool createConnection()
    15. {
    16. QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE");
    17. QSqlError err;
    18.  
    19. db.setDatabaseName("/home/pnunn/src/database/share/shares2.fdb");
    20. db.setHostName("localhost");
    21.  
    22. if (!db.open("SYSDBA","masterkey")) {
    23. err = db.lastError();
    24. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    25. err.text(), QMessageBox::Cancel);
    26. return false;
    27. }
    28.  
    29. return true;
    30. }
    31.  
    32. #endif
    To copy to clipboard, switch view to plain text mode 

    the function is called in main

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include "connection.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. if(!createConnection()) // try and open the database
    9. exit(1);
    10.  
    11. MainWindow w;
    12. w.show();
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    That makes the complete app. other than the UI which consists of a combobox and a table on a form (currently with a button that does nothing) [attached]

    Peter.
    Attached Files Attached Files

  4. #4
    Join Date
    Dec 2008
    Location
    Upper Ferntree Gully, Victoria, Australia
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Parameterized query not working.

    Still not winning with this one... I've even tried now using the anonymous parameter '?' as follows

    Qt Code:
    1. In constructor
    2.  
    3. 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"
    4. "where p.shareid = ?");
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. In function
    2.  
    3. query->addBindValue(shareidx.toInt());
    4. query->exec();
    To copy to clipboard, switch view to plain text mode 

    but query->lastQuery(); still has no parameter (returns as follows in the output)

    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 = ?


    Peter

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Parameterized query not working.

    did you try to execute this query in Qt's sqlbrowser which is located in QTDIR/demos/sqlbrowser?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Dec 2008
    Location
    Upper Ferntree Gully, Victoria, Australia
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Parameterized query not working.

    [red face]
    I have been trying to figure this out for ages... just installed qtcreator 1 and found a new error message from the running of the system.

    Turns out that I split the sql into two strings in the prepare statement and consequently missed a space between two key words...
    [/red face]

    Now works as advertised.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.