Results 1 to 11 of 11

Thread: Qt QSqlQuery scope issues

  1. #1
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Smile Qt QSqlQuery scope issues

    Hello

    I'm trying to follow procedure as stated in
    http://doc.qt.io/qt-4.8/qsqlquery.html

    I take this as reference
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("INSERT INTO person (id, forename, surname) "
    3. "VALUES (:id, :forename, :surname)");
    4. query.bindValue(":id", 1001);
    5. query.bindValue(":forename", "Bart");
    6. query.bindValue(":surname", "Simpson");
    7. query.exec();
    To copy to clipboard, switch view to plain text mode 

    This query item, in my case, is a pointer declared as member of a class.
    With this query pointer, I do in the class constructor, the following:


    Qt Code:
    1. mydb.setDatabaseName("BlaBla.db");
    2. mydb.open();
    3.  
    4. query = new QSqlQuery(mydb);
    5.  
    6. query->exec("create table MY_TABLE "
    7. "(column integer)");
    8.  
    9.  
    10. query->prepare("INSERT INTO MY_TABLE(column) VALUES(:holder)");
    To copy to clipboard, switch view to plain text mode 
    With mydb being initialized as QSqlDatabase::addDatabase("QSQLITE")

    Ok, so far so good, the table is created.
    Now, I want to use this query in a efficient way.
    I want to prepare it, and use it several times to add an entry to the database. That's why, at the end of the constructor, I prepare it already.

    So after I do this, I call a method that uses this query, from within the mainwindow thread. Please notice that the database is also created when the mainwindow thread is initialized.

    So, the method, inside the SAME class, is

    Qt Code:
    1. query->bindValue(":holder", receivedVal);
    2. if (query->exec())
    3. qDebug() << "Record Inserted";
    4. else
    5. qDebug() << "Record not inserted";
    To copy to clipboard, switch view to plain text mode 

    In this function I always get "Record not inserted"
    Now, for the funny fact:
    If, inside this method, I create a LOCAL query with the same command, the data will be successfully added. Problem is, I have to prepare it ALWAYS, and this is the reason it gets really slow, I believe. UI gets really slow and freezing.

    I've searched a lot already, but all I can find is scope ideas, that I believe I'm not missing.
    So, I appreciate your help

    Thank you

  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: Qt QSqlQuery scope issues

    What is the QSqlError you receive when the query->exec() returns false? Are you doing a query->finish() anywhere in your code? If you are, that will invalidate the prepared statement as well.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. The following user says thank you to jefftee for this useful post:

    Wer_Bn (14th October 2016)

  4. #3
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt QSqlQuery scope issues

    Hi jefftee,

    The error I get is:
    Qt Code:
    1. last Error: QSqlError("", "Parameter count mismatch", "")
    To copy to clipboard, switch view to plain text mode 

    I am not doing any finish(), and I can see that the „query string“ is still the one I defined on prepare:
    Qt Code:
    1. "INSERT INTO MY_TABLE(column) VALUES(:holder)"
    To copy to clipboard, switch view to plain text mode 

    Also I can see on the „variables and expressions“ field that the value passed on bind stays on the query structure.

    In images (here I have qry2 instead of query):
    Structure after prepare:
    After prepare.png
    Structure after bind:
    After bind.png
    Structure after exec:
    after exec.png

    Thanks
    Last edited by Wer_Bn; 13th October 2016 at 09:57.

  5. #4
    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: Qt QSqlQuery scope issues

    According to the SQLITE doc, "column" is a reserved keyword. Can you change the name of your column to a non-reserved keyword and see if that has anything to do with your error?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  6. #5
    Join Date
    Oct 2016
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt QSqlQuery scope issues

    Hi jefftee!

    I'm working with Wer_Bn in this project, and tried with a different name and the result is the same.

    after exec2.PNG

    Tanks

  7. #6
    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: Qt QSqlQuery scope issues

    In your first post, you show that you're creating the query on the heap and passing the db instance to the query constructor (which is correct). The QSqlDatabase instance seems to be allocated on the stack.

    Has your QSqlDatabase named mydb gone out of scope by chance?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  8. #7
    Join Date
    Oct 2016
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt QSqlQuery scope issues

    Hi,

    I checked "mydb" and is remains on he scope all the way.
    On constructor:
    mydb on contstructor.PNG
    Between bind and exec.
    mydb before exec.PNG

    Also tried to use a pointer to the data base on the constructor, and the result is the same.

    Regarding the Portability note on http://doc.qt.io/qt-5/qsqlquery.html#prepare:
    Some databases choose to delay preparing a query until it is executed the first time. In this case, preparing a syntactically wrong query succeeds, but every consecutive exec() will fail.
    To test this I've inserted successfully a first value on the database immediately after the prepare, on the constructor, to test if there is any error on query. So no syntactically error on query.


    I can not find any example of how to use the prepare on a first call, and the bind() & exec() repeatedly on another methods, although on http://doc.qt.io/qt-4.8/sql-sqlstatements.html says that:

    When inserting multiple records, you only need to call QSqlQuery:repare() once. Then you call bindValue() or addBindValue() followed by exec() as many times as necessary
    I am wondering if my approach is even possible...

    Thanks again,

  9. #8
    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: Qt QSqlQuery scope issues

    Quote Originally Posted by loccus View Post
    I am wondering if my approach is even possible...
    I am not a Qt 4.8 user, but I certainly prepare and subsequently bindValue/exec thousands of queries reusing the query that was previously prepared using Qt 5.x. Can you share more of your actual code? A problem like this is almost certainly going to be a head slapper once we find the problem...
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  10. #9
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt QSqlQuery scope issues

    We will try a simpler approach using the database directly on the MainWindow, instead of creating the database on a separate class.
    Then it will be simpler for you to analize the code.
    Then we will come and reply with the results
    Last edited by Wer_Bn; 17th October 2016 at 09:03.

  11. #10
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt QSqlQuery scope issues

    Meanwhile, we found a VERY interesting fact.

    We changed the database to the MainWindow class:
    - Database and query instance in the MainWindow Class (previously we had a database wrapper class)
    - Both variables are initialized in MainWindow Class constructor
    - query is being prepared in the contructor, after the database is created and opened
    - Upon a button pressed action, we had new entry to the database, through bind and exec. It TOTALLY works. We get as many entries as many times we press the button.

    So...
    Next step is to check if the use of DLL libraries is jeopardizing all of this. The class that used to have the database and query instances is in this DLL...
    Any feedback on this?

  12. #11
    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: Qt QSqlQuery scope issues

    Should be no issues with having code in your app exe or the DLL I wouldn't think. That said, I haven't had to use Windows in many years now, so perhaps someone else can chime in.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 2
    Last Post: 26th July 2016, 00:34
  2. Replies: 5
    Last Post: 26th April 2016, 18:59
  3. Replies: 2
    Last Post: 9th April 2013, 09:15
  4. Replies: 1
    Last Post: 18th July 2011, 12:12
  5. Strange issues with QSqlQuery
    By SIFE in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2011, 10:51

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.