简体   繁体   中英

Concurrency using Qt and SQLite databases

I'm working on a very simple small application which will be using Qt/SQLite. Sometimes the application will be accessing shared SQLite databases from multiple computers around the office.

Is there a way to detect (through events?) when an update has been made to the database from another computer on the network so the program can know to refresh their information?

Also, can connections to the DB be monitored by other computers (so they know who's editing the DB)?

Ideally, I don't want to create a separate table in the database for 'collaborative' purposes, and it's not worth setting up timers and loops just to monitor activity; I'd just like to know if there's anything built-in I could leverage to make sure everyone connected is always 'on the same page' in an efficient way.

As a last resort, would a Qt signal monitoring the SQLite database files' last modification time be a reliable way to track if there has been an update, or does the Qt SQLite driver tend to touch the database file outside of SQL transactions?

I agree that it'd be very nice if this were easy, but it isn't. SQLite doesn't provide any mechanisms to directly provide such functionality. The sqlite3_update_hook system only monitors one connection that must be within the monitoring process.

What you could do is to create a local distributed notification system. Roughly:

  1. Create an sql trigger that calls a custom SQLite function.

  2. The custom SQLite function posts an event to some notifier QObject . This needs to be done via an extern 'C' {...} stub.

  3. The notifier QObject broadcasts on the local subnet what has changed and where. This can be picked up by all of your applications running on the network.

  4. If you want to be really, really clever, you can have a custom proxy model on top of the sqlite model that receives the notifications and sends relevant signals.

This whole thing can be very, very simple, if you're after a particular case, not a general solution. It only sounds complicated :) If you want to see what might be involved otherwise, look here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM