Results 1 to 20 of 20

Thread: query prepare with sql math

  1. #1
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default query prepare with sql math

    This is my code:
    Qt Code:
    1. QSqlQuery query;
    2.  
    3. query.prepare("select :a / :b");
    4.  
    5. query.addBindValue( ui.a_lineEdit->text() );
    6. query.addBindValue( ui.b_lineEdit->text() );
    7.  
    8. query.exec();
    9.  
    10.  
    11. while (query.next()) {
    12. QString res = query.value(0).toString();
    13. ui.res_lineEdit->setText(res);
    To copy to clipboard, switch view to plain text mode 
    ui.a_lineEdit->text() is 9 and ui.b_lineEdit->text() is 3
    If I try to +,- or * the result shows up in ui.res_lineEdit->setText(res), but when I try to divide it doesnt show me anything. Why?

    Renan

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    What do query.exec() and query.lastError() return?

  3. #3
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    code:
    Qt Code:
    1. QSqlQuery query;
    2.  
    3. query.prepare("select :a / :b");
    4.  
    5. query.addBindValue( ui.a_lineEdit->text() );
    6. query.addBindValue( ui.b_lineEdit->text() );
    7.  
    8. query.exec();
    9. QMessageBox::critical(0, tr("Error"),
    10. QString("The error:\n%1").arg(query.lastError().text()));
    11.  
    12.  
    13. while (query.next()) {
    14. QString res = query.value(0).toString();
    15. ui.res_lineEdit->setText(res);
    To copy to clipboard, switch view to plain text mode 

    query.lastError() returns me nothing.

    What could I do to know what query.exec() returns?

    Renan

  4. #4
    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: query prepare with sql math

    exec() returns true or false. if exec() returns false that means that an error appeared during query executing.

  5. #5
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    Qt Code:
    1. QSqlQuery query;
    2.  
    3. query.prepare("select :a / :b");
    4.  
    5. query.addBindValue( ui.a_lineEdit->text() );
    6. query.addBindValue( ui.b_lineEdit->text() );
    7.  
    8. if (!query.exec()){
    9. QMessageBox::critical(0, tr("Error"),
    10. QString("There is an error with query.exec()!"));
    11. }else{
    12.  
    13. query.exec();
    14. QMessageBox::critical(0, tr("Error"),
    15. QString("There is no error with query.exec():\n%1").arg(query.lastError().text()));
    16.  
    17. while (query.next()) {
    18. QString res = query.value(0).toString();
    19. ui.res_lineEdit->setText(res);
    20. }
    To copy to clipboard, switch view to plain text mode 

    There is no error with query.exec() and query.lastError() returns me nothing.

    Any suggestions?

    Renan

  6. #6
    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: query prepare with sql math

    sorry, it works.

  7. #7
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    what query.exec() and query.lastError() returns?

  8. #8
    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: query prepare with sql math

    this code returns "2"
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setHostName("localhost");
    3. db.setDatabaseName("test.db");
    4. db.setUserName("system");
    5. db.setPassword("sysdba");
    6. if (!db.open())
    7. qWarning() << db.lastError().text();
    8. else {
    9. QSqlQuery query(db);
    10. query.prepare("CREATE TABLE test (a int, b int)");
    11. if (!query.exec())
    12. qWarning() << query.lastError().text();
    13. else {
    14. qDebug() << query.exec("INSERT INTO test (a, b) VALUES (1, 2)");
    15. qDebug() << query.exec("INSERT INTO test (a, b) VALUES (3, 4)");
    16. qDebug() << query.exec("INSERT INTO test (a, b) VALUES (5, 5)");
    17. query.prepare("SELECT :a / :b");
    18. query.addBindValue(4);
    19. query.addBindValue(2);
    20. qDebug() << query.exec();
    21. query.next();
    22. qDebug() << query.value(0);
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    I have tested your code and it returns "2".

    Something strange is happening, because when I change
    Qt Code:
    1. query.prepare("select :a / :b");
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. query.prepare("select :a + :b");
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. query.prepare("select :a - :b");
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. query.prepare("select :a * :b");
    To copy to clipboard, switch view to plain text mode 

    it returns me the right result.
    Do you see anything wrong with my code?


    Renan

  10. #10
    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: query prepare with sql math

    what kind of database do you use? mysql, right?

  11. #11
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    yes mysql.
    here is the the pushbutton code:
    Qt Code:
    1. void testeMYSQL2::on_soma_pushButton_clicked()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. db.setHostName(ui.hostname_lineEdit->text());
    5. db.setDatabaseName(ui.database_lineEdit->text());
    6. db.setUserName(ui.username_lineEdit->text());
    7. db.setPassword(ui.password_lineEdit->text());
    8. db.open();
    9. if (!db.open()){
    10. QMessageBox::critical(0, tr("Error"),
    11. QString("The error:\n%1").arg(db.lastError().text()));
    12. }
    13. else{
    14. QSqlQuery query;
    15.  
    16. query.prepare("SELECT :a / :b");
    17.  
    18. // query.addBindValue( ui.a_lineEdit->text() );
    19. // query.addBindValue( ui.b_lineEdit->text() );
    20. // query.addBindValue( 400 );
    21. // query.addBindValue( 2 );
    22. query.bindValue(":a", ui.a_lineEdit->text() );
    23. query.bindValue(":b", ui.b_lineEdit->text() );
    24.  
    25. if (!query.exec()){
    26. QMessageBox::critical(0, tr("Error"),
    27. QString("There is an error with query.exec()!"));
    28. }else{
    29.  
    30. // query.exec();
    31. QMessageBox::critical(0, tr("Error"),
    32. QString("There is NO error with query.exec():\n%1").arg(query.lastError().text()));
    33.  
    34. while (query.next()) {
    35. QString res = query.value(0).toString();
    36. ui.res_lineEdit->setText(res);
    37. }
    38. }
    39. }
    40. db.close();
    41. }
    To copy to clipboard, switch view to plain text mode 

    Renan

  12. #12
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    Here is something really strange:
    if I change:
    Qt Code:
    1. query.prepare("SELECT :a / :b");
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. query.prepare("SELECT (:a / :b)*1");
    To copy to clipboard, switch view to plain text mode 

    I get the result.
    Maybe a QT bug?

    Renan

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    Indeed it behaves strange. Could you prepare a minimal compilable example?

  14. #14
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    I have uploaded a simple compilable example, as jacek asked.

    Renan

    I have uploaded it again.
    Attached Files Attached Files
    Last edited by GuL; 2nd September 2008 at 15:00. Reason: changing testemysql.cpp

  15. #15
    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: query prepare with sql math

    I can't download testemysql2.cpp

  16. #16
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    I have uploaded it again.

    Renan

  17. #17
    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: query prepare with sql math

    looks like bug in Qt.
    this code works, you can use it
    Qt Code:
    1. ...
    2. query.prepare(QString("select %1/%2").arg(ui.lineEdit->text()).arg(ui.lineEdit_2->text()));
    3. ...
    To copy to clipboard, switch view to plain text mode 

  18. The following user says thank you to spirit for this useful post:

    GuL (2nd September 2008)

  19. #18
    Join Date
    Aug 2008
    Location
    Porto Alegre
    Posts
    65
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    Thanks again spirit.

    I will wait one or two days to report these to trolltech. Maybe someone knows why this is happening.

    I'm using QT 4.3.4 open source Linux Ubuntu 8.04. I don't if this happens with QT 4.4.1.

    Renan

  20. #19
    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: query prepare with sql math

    I've tested on Qt 4.4.1, the result is the same.

  21. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: query prepare with sql math

    I've minimized it a bit further (see attachment) and it appears that for Qt 4.4.1 on Linux I get the "No results." message, which means that the database doesn't return any data. Of course it works for * operator.

    I've also found a workaround:
    Qt Code:
    1. query.addBindValue( 4.0 );
    2. query.addBindValue( 2.0 );
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

Similar Threads

  1. QTableWidget Sql Query 25.000 records
    By aekilic in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2008, 14:54
  2. Sql Query Problem
    By MrShahi in forum Qt Programming
    Replies: 6
    Last Post: 30th May 2008, 09:16
  3. QTreeView with results from an SQL query.
    By ederbs in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2007, 02:41

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.