Page 1 of 2 12 LastLast
Results 1 to 20 of 26

Thread: QSqlQuery and QLineEdit

  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 QSqlQuery and QLineEdit

    Why this code doesn't work?
    I want to show the result of this query in the QLineEdit.

    Qt Code:
    1. void testeMYSQL2::on_pushButton_clicked()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. db.setHostName("localhost");
    5. db.setDatabaseName("teste");
    6. db.setUserName("root");
    7. db.setPassword("");
    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. QMessageBox::information(0, tr("OK"), QString("The is NO error\n"));
    15. }
    16. */
    17. QSqlQuery query;
    18. query.exec("select nome_prod from produtos where cod_prod = 1");
    19. query.next();
    20. for (int i = 0; i < record.count(); i++) {
    21. QString name = query.value(i).toString();
    22. nameEdit->setText(name);
    23. }
    To copy to clipboard, switch view to plain text mode 

    What I am doing wrong?

    Renan

  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: QSqlQuery and QLineEdit

    at first, you have only one column (according to your query text)
    second, you set position only on first record (query.next())
    and then try to read data from non-existed columns (you have only one column!)
    so, try this code
    Qt Code:
    1. ....
    2. while (query.next()) {
    3. QString data = query.value(0).toString();//first column == nome_prod
    4. ....
    5. }
    6. ....
    To copy to clipboard, switch view to plain text mode 

    read for detailes QSqlQuery::next

  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: QSqlQuery and QLineEdit

    The result of this query is only one line and one column...

    The result is DISCO1

    I just want to see this word in the QLineEdit, thats why I didn't use the while loop.
    Why the result doesn't show? What signal/slot should I use?
    Signal->clicked()
    Slot-> ???

    Renan
    Last edited by GuL; 14th August 2008 at 17:39.

  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: QSqlQuery and QLineEdit

    if you need only one result, then
    Qt Code:
    1. query.next();
    2. QString res = query.valut(0).toString();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: QSqlQuery and QLineEdit

    I don't know what "record" is in your case, but I'd guess it's an invalid QSqlRecord with a count of 0, thus your loop never gets executed. I'd suggest doing it the way spirit said.

  6. #6
    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: QSqlQuery and QLineEdit

    New code and still no result:

    Qt Code:
    1. void testeMYSQL2::on_pushButton_clicked()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. db.setHostName("localhost");
    5. db.setDatabaseName("teste");
    6. db.setUserName("root");
    7. db.setPassword("teste1234");
    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. QMessageBox::information(0, tr("OK"), QString("The is NO error\n"));
    15. }
    16. */
    17. QSqlQuery query;
    18. query.exec("select nome_prod from produtos where cod_prod = 1");
    19.  
    20. query.next();
    21. QString name = query.value(0).toString();
    22. nameEdit->setText(name);
    23. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    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: QSqlQuery and QLineEdit

    Qt Code:
    1. void testeMYSQL2::on_pushButton_clicked()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. db.setHostName("localhost");
    5. db.setDatabaseName("teste");
    6. db.setUserName("root");
    7. db.setPassword("teste1234");
    8. if (!db.open()) {
    9. qDebug() << query.lastError().text();
    10. return;
    11. }
    12.  
    13. QSqlQuery query;
    14. if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
    15. qDebug() << query.lastError().text();
    16. return;
    17. }
    18.  
    19. if (!query.next()) {
    20. qDebug() << query.lastError().text();
    21. return;
    22. }
    23. QString name = query.value(0).toString();
    24. nameEdit->setText(name);
    25. }
    To copy to clipboard, switch view to plain text mode 
    run this code and show console messages.

  8. #8
    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: QSqlQuery and QLineEdit

    gul@gul-laptop:~/qt/testemysql2$ ./testemysql2
    QSqlQuery::exec: database not open
    "Driver not loaded Driver not loaded"

    =-=-=-=-=-=-==-=-=-=-

    It doesn't show any messages:

    gul@gul-laptop:~/qt/testemysql2$ ./testemysql2
    Last edited by GuL; 14th August 2008 at 18:29.

  9. #9
    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: QSqlQuery and QLineEdit

    did you have compiled plugin or qt with mysql driver?

  10. #10
    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: QSqlQuery and QLineEdit

    It is working.

    Qt Code:
    1. #
    2. void testeMYSQL2::on_pushButton_clicked()
    3. {
    4. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    5. db.setHostName("localhost");
    6. db.setDatabaseName("teste");
    7. db.setUserName("root");
    8. db.setPassword("teste1234");
    9. if (!db.open()) {
    10. qDebug() << query.lastError().text();
    11. return;
    12. }
    13.  
    14. QSqlQuery query;
    15. if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
    16. qDebug() << query.lastError().text();
    17. return;
    18. ....
    19. }
    To copy to clipboard, switch view to plain text mode 

    this line, the fisrt one: qDebug() << query.lastError().text();

    should be: qDebug() << db.lastError().text();

    to compile.
    because it is testing its connection

    never mind., but it doesn't show anything to me.

  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: QSqlQuery and QLineEdit

    what can I do to get those lastError() ???

    Renan

  12. #12
    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: QSqlQuery and QLineEdit

    does qDebug() << db.lastError().text(); return nothing?
    if QSqlDatabase::lastError doesn't return anything then everything is ok.

  13. #13
    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: QSqlQuery and QLineEdit

    ok, but

    if everything is ok, why QLineEdit doesn't show the result?

    what about Signals and Slots?


    Renan

  14. #14
    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: QSqlQuery and QLineEdit

    Quote Originally Posted by GuL View Post
    ok, but
    if everything is ok, why QLineEdit doesn't show the result?
    you showed error message on console "driver not loaded". doesn't this error appear anymore?


    Quote Originally Posted by GuL View Post
    what about Signals and Slots?
    what slots and signals?

  15. #15
    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: QSqlQuery and QLineEdit

    Quote:
    Originally Posted by GuL View Post
    ok, but
    if everything is ok, why QLineEdit doesn't show the result?
    you showed error message on console "driver not loaded". doesn't this error appear anymore?
    No, it doesnt. I have changed your code in order to compile and that error happened. Than I changed it again and it worked!

    Quote:
    Originally Posted by GuL View Post
    what about Signals and Slots?
    what slots and signals?
    Should I connect pushbutton to qlineedit?
    if your answer is yes,
    Qt Code:
    1. QObject::connect(pushButton, SIGNAL(clicked()), nameEdit, SLOT( ????????? ));
    To copy to clipboard, switch view to plain text mode 

    what should be the slot for nameEdit? I'm using the Designer to create the interface.

    Renan

  16. #16
    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: QSqlQuery and QLineEdit

    for what purpose do you need this connection?

  17. #17
    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: QSqlQuery and QLineEdit

    I thought I would need it to make the result to show in the QLineEdit. -- Now I know it isn't necessary.

    But my problem still persist, no result in QLineEdit.

    Any ideas?

    Renan

  18. #18
    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: QSqlQuery and QLineEdit

    check the result of your query in sqlbrowser which is located in QTDIR/demos/sqlbrowser.

  19. #19
    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: QSqlQuery and QLineEdit

    Could you please say what exactly did you do to eliminate the "Driver not loaded" message?

  20. #20
    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: QSqlQuery and QLineEdit

    here is spirit code:
    Qt Code:
    1. void testeMYSQL2::on_pushButton_clicked()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. db.setHostName("localhost");
    5. db.setDatabaseName("teste");
    6. db.setUserName("root");
    7. db.setPassword("teste1234");
    8. if (!db.open()) {
    9. qDebug() << query.lastError().text();
    10. return;
    11. }
    12.  
    13. QSqlQuery query;
    14. if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
    15. qDebug() << query.lastError().text();
    16. return;
    17. }
    18.  
    19. if (!query.next()) {
    20. qDebug() << query.lastError().text();
    21. return;
    22. }
    23. QString name = query.value(0).toString();
    24. nameEdit->setText(name);
    25. }
    To copy to clipboard, switch view to plain text mode 

    its code didn't compile, than I changed to:
    Qt Code:
    1. void testeMYSQL2::on_pushButton_clicked()
    2. {
    3. QSqlQuery query;
    4. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    5. db.setHostName("localhost");
    6. db.setDatabaseName("teste");
    7. db.setUserName("root");
    8. db.setPassword("teste1234");
    9. if (!db.open()) {
    10. qDebug() << db.lastError().text();
    11. return;
    12. }
    13.  
    14. if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
    15. qDebug() << query.lastError().text();
    16. return;
    17. }
    18.  
    19. if (!query.next()) {
    20. qDebug() << query.lastError().text();
    21. return;
    22. }
    23. QString name = query.value(0).toString();
    24. nameEdit->setText(name);
    25. }
    To copy to clipboard, switch view to plain text mode 

    and then I got this error:
    gul@gul-laptop:~/qt/testemysql2$ ./testemysql2
    QSqlQuery::exec: database not open
    "Driver not loaded Driver not loaded"
    Then I saw where is the error:

    Qt Code:
    1. void testeMYSQL2::on_pushButton_clicked()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. db.setHostName("localhost");
    5. db.setDatabaseName("teste");
    6. db.setUserName("root");
    7. db.setPassword("teste1234");
    8. if (!db.open()) {
    9. qDebug() << db.lastError().text();
    10. return;
    11. }
    12.  
    13. QSqlQuery query;
    14. if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
    15. qDebug() << query.lastError().text();
    16. return;
    17. }
    18.  
    19. if (!query.next()) {
    20. qDebug() << query.lastError().text();
    21. return;
    22. }
    23. QString name = query.value(0).toString();
    24. nameEdit->setText(name);
    To copy to clipboard, switch view to plain text mode 

    Then it worked, but still I don't see the result in the QLineEdit.

    Now I will try what spirit said above. I will post the result in a few seconds.

    Renan

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.