Results 1 to 20 of 26

Thread: QSqlQuery and QLineEdit

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.

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
  •  
Qt is a trademark of The Qt Company.