[Interest] QSqlQuery

Mark Brand mabrand at mabrand.nl
Sun Jan 26 22:27:29 CET 2014


On 01/26/2014 09:24 PM, Igor Mironchik wrote:
>
>
>> Igor Mironchik schreef op 26-1-2014 16:29:
>>> Hi.
>>>
>>> I have some questions about database support in Qt 5.
>>>
>>> First of all, is it safe to transfer a QSqlQuery instance from one 
>>> thread to another to process him in another thread. I.e. I want to 
>>> execute SELECT query on "DB" thread and return a QSqlQuery from that 
>>> thread to the GUI thread, where I will navigate and show users the 
>>> result of the query.
>>>
>> No, that is not safe. You can use database access from threads, but 
>> every thread needs it own QSqlDatabase instance of the database 
>> connection. The documentation states:
>> "A connection can only be used from within the thread that created 
>> it. Moving connections between threads or creating queries from a 
>> different thread is not supported."
>> (http://qt-project.org/doc/qt-4.8/threads-modules.html#threads-and-the-sql-module)
>
> I will not use QSqlDatabase in another thread. I want to create 
> QSqlDatabase connection in worker thread and execute queries in that 
> thread... For SELECT statemetsa I want to return QSqlQuery object from 
> worker thread to the GUI one. Is it safe to use QSqlQuery in another 
> thread that created him? 

It's hard to imagine answering this question more clearly than those who 
have already responded, but this is absolutely not supported. Depending 
on which qsql driver you are using, you might get away with it for a 
while, but don't count on it. The QSqlQuery object carries the 
QSqlDatabase connection that it reads its data from, so if you access it 
from another thread you access the QSqlDatabase connection too.

Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140126/57112b80/attachment.html>


More information about the Interest mailing list