Results 1 to 7 of 7

Thread: QSqlQuery::bindValue problem

  1. #1
    Join Date
    Jan 2006
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSqlQuery::bindValue problem

    hi

    since the update to qt 4.5 i can't get QSqlQuery::bindValue to work
    i tried google and searched the forum but couldn't find an answer to this question.

    the following sample code works perfectly using qt 4.3.4, but with qt 4.5 i get this error:

    QSqlError(-1, "QPSQL: Unable to create query", "ERROR: syntax error at or near "(" at character 10")

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QSqlDatabase>
    3. #include <QSqlQuery>
    4. #include <QSqlError>
    5. #include <QDebug>
    6. #include <QVariant>
    7.  
    8.  
    9. int main(int argc, char **argv) {
    10. QCoreApplication a(argc, argv);
    11. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    12. db.setHostName("localhost");
    13. db.setDatabaseName("test");
    14. db.setUserName("testuser");
    15. db.setPassword("testpw");
    16. db.open();
    17.  
    18. QSqlQuery q(db);
    19. q.prepare("SELECT :bla FROM myTable");
    20. q.bindValue(":bla","foo");
    21. if(q.exec()) {
    22. while(q.next()) {
    23. qDebug() << "output:" << q.value(0).toString();
    24. }
    25. }
    26. else {
    27. qDebug() << q.lastError();
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    if i do a
    Qt Code:
    1. q.prepare("SELECT foo FROM myTable");
    To copy to clipboard, switch view to plain text mode 
    everything works just fine :/ but i can't get bindValue to work in qt 4.5
    does anyone know what's the problem here?

  2. #2
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::bindValue problem

    Im not sure if this ought to work, since I always use value() method to get the selected columns.

    If it shoud work at all, it might have to be bound as "out" or inout variable, the default is however QSql::in

    In case your intention is to dynamically change column names of the query, maybe this is a solution:

    query.prepare(QString("select %1 from mytable").arg("foo"));
    Last edited by seneca; 10th March 2009 at 15:32.

  3. The following user says thank you to seneca for this useful post:

    segfault (10th March 2009)

  4. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::bindValue problem

    Quote Originally Posted by segfault View Post
    if i do a
    Qt Code:
    1. q.prepare("SELECT foo FROM myTable");
    To copy to clipboard, switch view to plain text mode 
    everything works just fine :/ but i can't get bindValue to work in qt 4.5
    does anyone know what's the problem here?
    But I think that after bindValue Your query looks like :
    Qt Code:
    1. SELECT 'foo' FROM myTable
    To copy to clipboard, switch view to plain text mode 
    Try look at PostgreSQL logs.

  5. #4
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::bindValue problem

    That seems to be right.

    Maybe the OP will make clear what he wants to achive.

  6. #5
    Join Date
    Jan 2006
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQuery::bindValue problem

    Quote Originally Posted by Lesiok View Post
    But I think that after bindValue Your query looks like :
    Qt Code:
    1. SELECT 'foo' FROM myTable
    To copy to clipboard, switch view to plain text mode 
    Try look at PostgreSQL logs.


    Qt Code:
    1. SELECT 'foo' FROM myTable
    2. or
    3. SELECT foo FROM myTable
    To copy to clipboard, switch view to plain text mode 

    doesn't matter, since they work both.


    my problem is that i used
    Qt Code:
    1. QSqlQuery q(db);
    2. q.prepare("SELECT :bla FROM test");
    3. q.bindValue(":bla",someVariable);
    To copy to clipboard, switch view to plain text mode 

    in a project using qt < 4.5 to create some select queries and it doesn't work in qt 4.5 anymore, it works fine for insert statements though.

    but since a solution like
    Qt Code:
    1. query.prepare(QString("select %1 from mytable").arg("foo"));
    To copy to clipboard, switch view to plain text mode 

    works fine, i will use this. (should have done it like that in the first place)

    thanks everybody for your help.

  7. #6
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::bindValue problem

    Quote Originally Posted by segfault View Post
    Qt Code:
    1. SELECT 'foo' FROM myTable
    2. or
    3. SELECT foo FROM myTable
    To copy to clipboard, switch view to plain text mode 

    doesn't matter, since they work both.
    Yes they both work, but they do different things of course.

    The first will always return the text foo, no matter what is actually in the table, and the second will return the content of the column with name foo.

  8. #7
    Join Date
    Jan 2006
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQuery::bindValue problem

    Quote Originally Posted by seneca View Post
    Yes they both work, but they do different things of course.

    The first will always return the text foo, no matter what is actually in the table, and the second will return the content of the column with name foo.
    i know sorry, i just wanted to point out that i would be happy with either of them, instead of the error i got

Similar Threads

  1. Compilation problem with QSqlQuery::bindValue()
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 18th November 2013, 22:30
  2. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  3. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  4. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.