Results 1 to 7 of 7

Thread: QSQLQUERY commands

  1. #1

    Default QSQLQUERY commands

    Hi

    I have the following code :

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3.  
    4. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    5. db.setDatabaseName("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=d:/");
    6. qDebug() << db.open();
    7.  
    8. qDebug() << db.tables();
    9.  
    10. QSqlQuery query = QSqlQuery("SELECT * FROM dzialka", db);
    11.  
    12.  
    13. query.prepare("INSERT INTO test(P1)"
    14. "VALUES (9999)");
    15. query.exec();
    16.  
    17. while (query.next()) {
    18. QString test = query.value(0).toString();
    19. qDebug () << test;
    20. }
    21.  
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    Why If I add the lines :

    Qt Code:
    1. query.prepare("INSERT INTO test(P1)"
    2. "VALUES (9999)");
    3. query.exec();
    To copy to clipboard, switch view to plain text mode 

    The lopp while does not work ? because the "curosor" is set on the last posiotion ?

    Regards
    Artur
    Last edited by arturs; 10th June 2016 at 18:55.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QSQLQUERY commands

    Your insert query won't return multiple rows in a result set. In fact, it won't return any, so why do you think the while loop should execute multiple times?

    hint: You have a select in the QSqlQuery constructor, but you then later prepare and execute an insert statement on the same QSqlQuery instance.
    Last edited by jefftee; 10th June 2016 at 22:58.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3

    Default Re: QSQLQUERY commands

    Quote Originally Posted by jefftee View Post
    Your insert query won't return multiple rows in a result set. In fact, it won't return any, so why do you think the while loop should execute multiple times?
    Yes I know. Please take a look once again on my code.
    If I remove the following lines from my code (the lines there are before while looop):

    Qt Code:
    1. query.prepare("INSERT INTO test(P1)"
    2. "VALUES (9999)");
    3. query.exec();
    To copy to clipboard, switch view to plain text mode 

    Everything works. All records from test tables are showed but If I add the lines then records from table are not showed.

    I would like to know Why ? because "cursor" is set at last record ?

    Regards
    Artur
    Last edited by arturs; 11th June 2016 at 08:56.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSQLQUERY commands

    You have already been told. You are trying to iterate over the rows returned through the query object. That last thing that query object executed was an SQL Insert, which returns no rows.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  5. #5

    Default Re: QSQLQUERY commands

    I have already corrected my post. I used wrong tag by mistake.

    I thought that I was over the row but I was not sure.

    What should I do to display all records once again ?
    Is it necessary to use command SELECT once again or use another way to set "cursor" at first records ?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSQLQUERY commands

    Quote Originally Posted by arturs View Post
    What should I do to display all records once again ?
    You issue a SELECT query,

    Quote Originally Posted by arturs View Post
    Is it necessary to use command SELECT once again or use another way to set "cursor" at first records ?
    You are currently not executing any SELECT query.

    Cheers,
    _

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSQLQUERY commands

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    2. db.setDatabaseName("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=d:/");
    3.  
    4. QSqlQuery insertQry("INSERT INTO test(P1). VALUES (9999)", db); // one query object, prepared and executed.
    5. insertQry.prepare("INSERT INTO test(P1) VALUES(9998);"); // same object reused
    6. insertQry.exec();
    7. insertQry.prepare("INSERT INTO test(P1) VALUES(?);"); // same object reused again
    8. for (int i = 9990; i < 9996: ++i) {
    9. insertQry.bindValue(i);
    10. insertQry.exec();
    11. }
    12.  
    13. QSqlQuery query = QSqlQuery("SELECT * FROM dzialka", db); // a different query object
    14.  
    15. while (query.next()) {
    16. QString test = query.value(0).toString();
    17. qDebug () << test;
    18. }
    To copy to clipboard, switch view to plain text mode 
    You also need to be aware that an SQL database engine will present results at a consistent point in time. So if you execute a select, insert rows (using a separate query object or external program), and then fetch the results of the select you will generally only see the rows at the time of the select execution. I am not sure if the Microsoft ODBC-dBase bridge maintains that read consistency.

Similar Threads

  1. QSqlQuery doesn't return all results
    By ce_nort in forum Newbie
    Replies: 2
    Last Post: 10th May 2016, 16:45
  2. Why does QSqlQuery::prepare() return false?
    By NotANoob in forum Qt Programming
    Replies: 2
    Last Post: 20th April 2011, 19:05
  3. QSqlQuery don't return
    By ShadowBelmolve in forum Newbie
    Replies: 2
    Last Post: 9th September 2010, 20:28
  4. QSqlQuery return result
    By arpspatel in forum Qt Programming
    Replies: 2
    Last Post: 9th April 2010, 07:55
  5. QSqlQuery always return junk value while for varchar oracle.
    By ranjit2709 in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2007, 05:19

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.