Results 1 to 6 of 6

Thread: Strange issues with QSqlQuery

  1. #1
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Strange issues with QSqlQuery

    I have this SLOT:
    Qt Code:
    1. void ConfSetup::setNextPage()
    2. {
    3. int currentIndex = ui->stackedWidget->currentIndex();
    4.  
    5. switch(currentIndex)
    6. {
    7. case 1:
    8. {
    9. ui->stackedWidget->setCurrentIndex(currentIndex + 1);
    10. }
    11. break;
    12.  
    13. case 2:
    14. {
    15. db = QSqlDatabase::addDatabase("QMYSQL");
    16. db.setDatabaseName("mysql");
    17. db.setHostName(ui->serverEdit->text());
    18. db.setPort(ui->portEdit->text().toInt());
    19. db.setUserName(ui->userEdit->text());
    20. db.setPassword(ui->passwordEdit->text());
    21.  
    22. if(!db.open())
    23. {
    24. QMessageBox::critical(0, trUtf8("Fail to login"), trUtf8("Wrong user or password"));
    25.  
    26. }
    27. else
    28. {
    29. ui->stackedWidget->setCurrentIndex(currentIndex + 1);
    30. }
    31. }
    32. break;
    33.  
    34. case 3:
    35. {
    36. QSqlQuery query;
    37. query.prepare("SELECT user FROM user WHERE user=:user");
    38. // query.prepare("SELECT user FROM user WHERE user='root'");
    39. query.bindValue(":user", ui->userDbEdit->text());
    40. query.exec();
    41.  
    42. if(query.numRowsAffected() > 0)
    43. QMessageBox::critical(0, trUtf8("User exist"), trUtf8("This user already token"));
    44.  
    45. else
    46. {
    47. qDebug("%s" , query.executedQuery().toStdString().c_str());
    48. qDebug() << db.databaseName();
    49. qDebug() << db.isOpen();
    50. qDebug() << query.lastError();
    51. qDebug("%s" , query.lastQuery().toStdString().c_str());
    52. qDebug("%d" , query.isActive());
    53. qDebug("%s" , db.driverName().toStdString().c_str());
    54. qDebug() << db.lastError();
    55. ui->stackedWidget->setCurrentIndex(currentIndex + 1);
    56. }
    57. }
    58. break;
    59.  
    60. case 4:
    61. {
    62. QSqlQuery query;
    63. query.prepare("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=:database");
    64. query.bindValue(":database", ui->dbNameEdit->text());
    65. query.exec();
    66.  
    67. if(query.numRowsAffected() == 1)
    68. QMessageBox::critical(0, trUtf8("Fail to create database"), trUtf8("This database already exist"));
    69.  
    70. else
    71. ui->stackedWidget->setCurrentIndex(currentIndex + 1);
    72. }
    73. break;
    74.  
    75. case 5:
    76. {
    77. QSqlQuery query;
    78. query.prepare("INSERT INTO Users(User, Password)" \
    79. "VALUES(:user, :password)");
    80. query.bindValue(":user", ui->userEdit->text());
    81. query.bindValue(":password", ui->passwordEdit->text());
    82. query.exec();
    83. ui->stackedWidget->setCurrentIndex(currentIndex + 1);
    84. }
    85. break;
    86.  
    87. }
    88. // ui->comboBox->addItem(ui->userDbEdit->text());
    89. // ui->lineEdit->setText(addSlashes(ui->slashesEdit->text()));
    90. }
    To copy to clipboard, switch view to plain text mode 
    And the connection like so:
    Qt Code:
    1. connect(ui->nextButton0, SIGNAL(clicked()), this, SLOT(setNextPage()));
    2. connect(ui->nextButton1, SIGNAL(clicked()), this, SLOT(setNextPage()));
    3. //etc
    To copy to clipboard, switch view to plain text mode 
    The query is correct because I test it in phpMyAdmin, qDebug output show this:
    Qt Code:
    1. SELECT user FROM user WHERE user=? //should be ... user = user, but formated like this because MySQL doesn't support native prepared statment
    2. "mysql"
    3. true
    4. QSqlError(-1, "", "")
    5. SELECT user FROM user WHERE user=:user
    6. 1
    7. QMYSQL
    8. QSqlError(-1, "", "")
    To copy to clipboard, switch view to plain text mode 
    Even if I execute this query:
    Qt Code:
    1. query.prepare("SELECT user FROM user WHERE user='root'");
    To copy to clipboard, switch view to plain text mode 
    query still return -1.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Strange issues with QSqlQuery

    What do you mean that query returns -1? If you mean QSqlError(-1,"", "") then it means there is no error. Does your query get executed properly? Using numRowsAffected() with SELECT statements doesn't make sense, even the docs state that so don't rely on it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange issues with QSqlQuery

    My query executed properly, my problem with numRowsAffected() always return -1, is there a way to get number of rows has been returned.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Strange issues with QSqlQuery

    Yes, read the docs of the methods you are using. Besides, if you are interested in the number of rows, then ask the database about the number of rows and not about rows themselves.

    sql Code:
    1. SELECT COUNT(*) FROM user WHERE user='root'
    To copy to clipboard, switch view to plain text mode 

    By the way, are you sure the query you posted is really valid? You have a table called "user" that has a column that's also called "user"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange issues with QSqlQuery

    I found size method return number of rows founded.
    By the way, are you sure the query you posted is really valid? You have a table called "user" that has a column that's also called "user"?
    Yes, its valid, this is mysql database that chiped with MySQL server, you can give a table and a column some name.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Strange issues with QSqlQuery

    If you are only interested in the number of rows matching the criteria then don't use size() but rather COUNT(*). Then you'll always get one row with the number of entries instead of getting multiple rows which is less efficient.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Using QSqlQuery
    By darkman_dev in forum Newbie
    Replies: 2
    Last Post: 4th February 2011, 21:40
  2. QSqlQuery
    By yasher in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2010, 14:25
  3. QSqlQuery in PyQt4
    By wirasto in forum Newbie
    Replies: 5
    Last Post: 11th January 2010, 08:25
  4. QSqlquery
    By codeman in forum Qt Programming
    Replies: 10
    Last Post: 4th June 2009, 12:57
  5. what is going on a QSqlQuery?
    By mismael85 in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2008, 13:35

Tags for this Thread

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.