Results 1 to 11 of 11

Thread: Keep data in QSqlQueryModel when server is offline

  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Keep data in QSqlQueryModel when server is offline

    Hi!

    I'm building an application which relies heavily on communication with external MySQL databases.
    I haven't really discovered until recently that the query set in the model has to be open in order to provide the data to the model.
    This means (to the user) that also the connection to the DBMS will have to be up for the table to display the data from the last query.

    I've read the docs on QSqlQuery but I must say that I'm really not quite clear on why it has to stay open to stay valid, I mean it doesn't automatically update or anything, right?

    Any way, what I would like to do is for the table in my user interface would keep the data even if the internet/DBMS-connection gets lost.

    I guess one way would be to transfer the data that is to be shown to a temporary, local SQLite table but that sounds a little bulky and I'm guessing that shouldn't be necessary?

    Thanks!
    /Totte

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keep data in QSqlQueryModel when server is offline

    bool QSqlTableModel::select () [virtual]
    Populates the model with data from the table that was set via setTable(), using the specified filter and sort condition, and returns true if successful; otherwise returns false.
    After you called select(), fetched data are stored in your model until you don't call select() again.
    A camel can go 14 days without drink,
    I can't!!!

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

    Tottish (26th April 2011)

  4. #3
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keep data in QSqlQueryModel when server is offline

    Humm, bummer. I'm using a customized QSqlQueryModel where there is no select() method. =(

    /Tottish

  5. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keep data in QSqlQueryModel when server is offline

    Quote Originally Posted by Tottish View Post
    Humm, bummer. I'm using a customized QSqlQueryModel where there is no select() method. =(
    Sorry, my answer is wrong.
    You're using QSqlQueryModel instead of QSqlTableModel.
    In this case you use QSqlQueryModel::setQuery to obtain the same result
    A camel can go 14 days without drink,
    I can't!!!

  6. #5
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keep data in QSqlQueryModel when server is offline

    Yeah, I know how to set the query except:
    "Note that the query must be active and must not be isForwardOnly()."
    And I suppose that is why the data can't be displayed when the db-connection goes down.
    /Tottish

  7. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keep data in QSqlQueryModel when server is offline

    OK.

    With QSqlTableModel the result is the same because QSqlQueryModel::select use setQuery.
    As solution you can use a QAbstractTableModel where you store the result af the query
    A camel can go 14 days without drink,
    I can't!!!

  8. #7
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keep data in QSqlQueryModel when server is offline

    Thank you for replying but I'm not sure I'm following.
    I don't think there is a method called QSqlQueryModel::select and if there is I can't find it.

    Could you try to explain in more detail how I would go about saving my query in a QAbstractTableModel?

    If anyone else have other opinions on how this should best be preformed, all input is welcome.

    Thanks!
    /Tottish

  9. #8
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keep data in QSqlQueryModel when server is offline

    Quote Originally Posted by Tottish View Post
    I don't think there is a method called QSqlQueryModel::select and if there is I can't find it.
    Sorry, I means QSqlTableModel::select()

    However, when you have QSqlTableModel/QSqlQueryModel you can navigate it and add a row in your QAbstractTableModel for each record in QSqlxxModel
    A camel can go 14 days without drink,
    I can't!!!

  10. #9
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keep data in QSqlQueryModel when server is offline

    Ah, I think I see now, but wouldn't such an operation be really expensive if the table has a couple of hundred rows and say 25 columns (as it is in my case)?
    How fast would the interaction be with such a table compared to creating a SQLite table and "copy" the retrived query into that?
    Anyway, I'll check it out!
    Thanks for the tip!
    /Tottish

  11. #10
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keep data in QSqlQueryModel when server is offline

    Using "in memory" model (I think) is faster than using a "file-system" model (like SQLITE). You can also create an "in memory" SQLITE database.
    Depends from the "size" of the data that you retrieve
    A camel can go 14 days without drink,
    I can't!!!

  12. The following user says thank you to mcosta for this useful post:

    Tottish (28th April 2011)

  13. #11
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Keep data in QSqlQueryModel when server is offline

    OK mcosta, I'll go with the in memory model then. The data shouldn't be very bulky.
    Thanks a lot!
    /Tottish

Similar Threads

  1. QSqlQueryModel data update
    By psi in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 03:59
  2. Server data exchange strategy
    By thru in forum General Programming
    Replies: 3
    Last Post: 21st July 2010, 20:15
  3. a faster QSqlQueryModel::data()
    By baray98 in forum Qt Programming
    Replies: 0
    Last Post: 24th September 2009, 00:56
  4. Replies: 1
    Last Post: 19th March 2009, 09:20
  5. Different data types in a QSqlQueryModel
    By Lodas in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2008, 16:31

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.