PDA

View Full Version : Keep data in QSqlQueryModel when server is offline



Tottish
26th April 2011, 15:26
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

mcosta
26th April 2011, 16:07
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.

Tottish
26th April 2011, 16:18
Humm, bummer. I'm using a customized QSqlQueryModel where there is no select() method. =(

/Tottish

mcosta
26th April 2011, 16:25
Humm, bummer. I'm using a customized QSqlQueryModel where there is no select() method. =(


Sorry, my answer is wrong. :o
You're using QSqlQueryModel instead of QSqlTableModel.
In this case you use QSqlQueryModel::setQuery to obtain the same result

Tottish
26th April 2011, 16:30
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

mcosta
26th April 2011, 16:41
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

Tottish
27th April 2011, 17:26
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

mcosta
28th April 2011, 08:26
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

Tottish
28th April 2011, 08:33
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

mcosta
28th April 2011, 09:11
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

Tottish
28th April 2011, 09:14
OK mcosta, I'll go with the in memory model then. The data shouldn't be very bulky.
Thanks a lot!
/Tottish