Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26

Thread: QSqlQuery and QLineEdit

  1. #21
    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:

    DISCO1

    Any ideas?

    Renan

  2. #22
    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

    ok. so, when you call your slot console hasn't any messages, right?

  3. #23
    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

    here is small example
    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include <QtSql>
    4.  
    5. int main(int argc, char ** argv)
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    10. db.setDatabaseName("test.db");
    11.  
    12. if (!db.open()) {
    13. qDebug() << db.lastError().text();
    14. return 0;
    15. }
    16.  
    17. QSqlQuery query;
    18. qDebug() << query.exec("CREATE TABLE test (id int PRIMARY KEY, test nvarchar)");
    19.  
    20. qDebug() << query.exec("INSERT INTO test VALUES(0, 'a')");
    21. qDebug() << query.exec("INSERT INTO test VALUES(1, 'b')");
    22. qDebug() << query.exec("INSERT INTO test VALUES(2, 'c')");
    23. qDebug() << query.exec("INSERT INTO test VALUES(3, 'd')");
    24. qDebug() << query.exec("SELECT test FROM test WHERE id = 1");
    25. if (!query.next()) {
    26. qDebug() << query.lastError().text();
    27. return 0;
    28. }
    29. QLineEdit le(query.value(0).toString());
    30. le.show();
    31.  
    32. return app.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

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

    GuL (15th August 2008)

  5. #24
    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

    Quote Originally Posted by GuL View Post
    its code didn't compile, than I changed to:
    If the code didn't compile, how come you managed to run an non-existing program? "Driver not loaded" is a runtime error, not compiler message.

  6. #25
    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

    Copying spirit code to my project didn't compile. here is spirit original 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. QSqlQuery query;
    13. if (!query.exec("select nome_prod from produtos where cod_prod = 1")) {
    14. qDebug() << query.lastError().text();
    15. return;
    16. }
    17. if (!query.next()) {
    18. qDebug() << query.lastError().text();
    19. return;
    20. }
    21. QString name = query.value(0).toString();
    22. nameEdit->setText(name);
    23. }
    To copy to clipboard, switch view to plain text mode 

    In line 9, when we test if the database was successfully open,
    Qt Code:
    1. query.lastError().text();
    To copy to clipboard, switch view to plain text mode 
    I got this error:

    .\testemysql2.cpp(50) : error C2065: 'query' : undeclared identifier
    .\testemysql2.cpp(50) : error C2228: left of '.lastError' must have class/struct/union
    type is ''unknown-type''

    when I canged line 9 to:
    Qt Code:
    1. db.lastError().text();
    To copy to clipboard, switch view to plain text mode 
    I managed to run the program.

    About the run-time error: "Driver not loaded"
    It happened when I cut line 12 and paste it between line 2 and 3 and line 9 still was query.lastError().text();

    I will try your code from the last post, spirit.
    I had just install sqlite.

    Renan



    Renan

  7. #26
    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 shows me the letter "b" in a QLineEdit. Nice.

    I will try to modify my code to work with mysql using your example.

    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.