Background Database Tasks
-
I'm new to threaded applications and applications doing periodic tasks in the background (even in the same thread). I understand that the QSql classes can only be used in the thread the connection was created.
In my application there are a few places where it syncs data from the Internet. Thus, it downloads a large amount of data from the Internet, parses that data and inserts or updates records in my database (SQLite3). I'd like for this to happen in the background, in another thread. Is there a best practice for this type of operation? Can SQLite have multiple connections open? What happens if Thread A does:
@
If JohnNotFound Then
INSERT INTO abc (name) VALUES ('John')
@and Thread B also does the same operation at the same time? How do you handle that, assuming there should only be one John.
-
AFAIR sqlite blocks db for data changing operations.
As for general answer (not only for sqlite or database, but for any threaded operations) you can use mutexes or queue with operations and execute them one a time.