Results 1 to 14 of 14

Thread: QSqlQuery not working

  1. #1
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QSqlQuery not working

    I'm stucked at this problem since a very long time ( about one day or more ), I just can't find what's wrong! it looks so simple and until now everything in the project did work fine, relational tables, and etc, well, I isolated it in a function, may I explain first, I've loaded a QComboBox with a QSqlQueryModel with this query: "SELECT name FROM Categories", ok. Then I need to insert the id of the selected item in another table (for using as relation), I made this function:

    Qt Code:
    1. int ProductsForm::categoryIdByName(const QString& name)
    2. {
    3. QSqlQuery categories = QSqlQuery(model->database());
    4. categories.prepare("SELECT id FROM Categories "
    5. "WHERE name=:name)");
    6. categories.bindValue(":name",name);
    7. categories.exec();
    8. if(categories.size()==-1)
    9. {
    10. qDebug() << "error";
    11. return 1;
    12. }
    13. return cate
    To copy to clipboard, switch view to plain text mode 

    I call it with QComboBox::currentText(), BUT, as you see my concern there, query size aways return -1, no item! i've tried a lot of other querys, like "SELECT * FROM Categories", but nothing! I'm SURE the database, tables and items where created and inserted respectively, the program did work fine until this, I hope you guys can enlight-me and help me kick off this meaningless error. thanks in advance, sorry my bad english.
    Last edited by BrainStorm; 15th August 2010 at 01:59. Reason: spelling corrections

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: QSqlQuery not working

    Try to identify the problem using QSqlQuery::lastError().
    Last edited by saa7_go; 15th August 2010 at 02:10. Reason: updated contents

  3. #3
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery not working

    Quote Originally Posted by saa7_go View Post
    Try to identify the problem using QSqlQuery::lastError().
    it returns -1, no error =T

  4. #4
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: QSqlQuery not working

    If you modify your codes to the following:
    Qt Code:
    1. int ProductsForm::categoryIdByName(const QString& name)
    2. {
    3. QSqlQuery categories(model->database());
    4. categories.exec(QString("SELECT id FROM Categories WHERE name='%1'").arg(name));
    5. if(categories.size()==-1)
    6. {
    7. qDebug() << "error";
    8. return 1;
    9. }
    10. .....
    11. }
    To copy to clipboard, switch view to plain text mode 

    is categories.size() value still -1 ?

  5. The following user says thank you to saa7_go for this useful post:

    BrainStorm (15th August 2010)

  6. #5
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery not working

    Quote Originally Posted by saa7_go View Post
    If you modify your codes to the following:
    Qt Code:
    1. int ProductsForm::categoryIdByName(const QString& name)
    2. {
    3. QSqlQuery categories(model->database());
    4. categories.exec(QString("SELECT id FROM Categories WHERE name='%1'").arg(name));
    5. if(categories.size()==-1)
    6. {
    7. qDebug() << "error";
    8. return 1;
    9. }
    10. .....
    11. }
    To copy to clipboard, switch view to plain text mode 

    is categories.size() value still -1 ?
    yesss...

  7. #6
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery not working

    Qt Code:
    1. int ProductsForm::categoryIdByName(const QString& name)
    2. {
    3. QSqlQuery categories = QSqlQuery(model->database());
    4. categories.exec(QString("SELECT id FROM Categories WHERE name='%1'").arg(name));
    5. categories.first();
    6. return categories.value(0).toInt();
    To copy to clipboard, switch view to plain text mode 

    it works now, and i still don't know why! ^^" but thanks anyway

  8. #7
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: QSqlQuery not working

    Are you using sqlite database? If you are using sqlite, it doesn't support reporting information about query sizes. So, this is why you always get -1.

  9. #8
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery not working

    Quote Originally Posted by saa7_go View Post
    Are you using sqlite database? If you are using sqlite, it doesn't support reporting information about query sizes. So, this is why you always get -1.
    yes, i'm using sqlite

  10. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlQuery not working

    Quote Originally Posted by BrainStorm View Post
    yes, i'm using sqlite
    then you can check via
    Qt Code:
    1. QSqlQuery categories = QSqlQuery(model->database());
    2. categories.prepare("SELECT id FROM Categories "
    3. "WHERE name=:name)");
    4. categories.bindValue(":name",name);
    5. categories.exec();
    6. if (!categories.next())
    7. {
    8. // error and return;
    9. }
    10.  
    11. categories.value(0)/*...*/
    To copy to clipboard, switch view to plain text mode 

  11. #10
    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 not working

    Is this code "copy and paste" ? If YES, You have an error in line 2. Signe ) after :name.

  12. #11
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery not working

    Quote Originally Posted by Lykurg View Post
    then you can check via
    Qt Code:
    1. QSqlQuery categories = QSqlQuery(model->database());
    2. categories.prepare("SELECT id FROM Categories "
    3. "WHERE name=:name)");
    4. categories.bindValue(":name",name);
    5. categories.exec();
    6. if (!categories.next())
    7. {
    8. // error and return;
    9. }
    10.  
    11. categories.value(0)/*...*/
    To copy to clipboard, switch view to plain text mode 
    i see.. but i think using QSqlQuery::next(); once is the same as using QSqlQuery::first();

  13. #12
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery not working

    By the way, does anyone knows how to create a button (flat would be nice) that drops a drop menu?
    Last edited by BrainStorm; 16th August 2010 at 01:43. Reason: spelling corrections

  14. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlQuery not working

    Quote Originally Posted by BrainStorm View Post
    i see.. but i think using QSqlQuery::next(); once is the same as using QSqlQuery::first();
    Yes, missed that, since in the code snippet the return was not checked if it is valid.

    Quote Originally Posted by BrainStorm View Post
    By the way, does anyone knows how to create a button (flat would be nice) that drops a drop menu?
    Use a normal buttun and set a menu via QPushButton::setMenu(). for flat use QPushButton::setFlat().

  15. The following user says thank you to Lykurg for this useful post:

    BrainStorm (16th August 2010)

  16. #14
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery not working

    Quote Originally Posted by Lykurg View Post
    Use a normal buttun and set a menu via QPushButton::setMenu(). for flat use QPushButton::setFlat().
    Ohh.. i see ^^ thanks, i was using QMenu::popup() with QPushButton::pos() :p

Similar Threads

  1. QSqlQuery
    By yasher in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2010, 15:25
  2. QSqlquery
    By codeman in forum Qt Programming
    Replies: 10
    Last Post: 4th June 2009, 13:57
  3. what is going on a QSqlQuery?
    By mismael85 in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2008, 14:35
  4. QSqlQuery problem
    By MarkoSan in forum Qt Programming
    Replies: 14
    Last Post: 14th January 2008, 23:50
  5. QSqlQuery error
    By skuda in forum Qt Programming
    Replies: 2
    Last Post: 2nd November 2007, 09:43

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.