0

I am writing a QT application which monitors some statistics. In the main window you choose multiple (or just one) items and open a graph window for them. Each item is polled from a different thread.

Every time I get data, it is written in a SQLite database, but I have encountered a problem:

I am making this application on a computer with a SSD drive and it runs OK, but when I run it on a computer with a HDD the application crashes (the crash happens in the QT sqlite dll file - qsqlite.dll). I googled the problem and have found many people saying that sqlite is not recommended for such usage. I have also used QMutex to lock/unlock the sections when I write to the DB, but unfortunately it does not fix the problem.

Is there a way that I can use sqlite for this or must I look for a different database such as postgres ?

Thank you for your time !

user2195430
  • 105
  • 1
  • 8

1 Answers1

2

I've never been a fan of multiple database connections within one application.

In your situation, I would look to implement a queue (FIFO) for all the write-SQL statements and send them to the database through only one connection. As you will be writing to the queue from several threads you will have to use mutexes etc to protect the write to the queue.

This way, the only thread contention is in your code and the SQLite drivers don't have to work too hard.

I would also consider caching the data from the read-queries so you are only requesting data that you don't have already. That is, request, say, the most recent 100 values and keep them in an array or list, then when you next request values, only request the values from the last one you have in the array.

Michael Vincent
  • 1,620
  • 1
  • 20
  • 46
  • Thank you for the idea. It seems to work perfectly in my situation (Will need some more testing). I made it like this: I check every 10 seconds if there are items in a queue (QString vector). If they are, I put each item in a QSQLQuery and execute them (FIFO). To add items to queue I use signal/slot. I use a mutex when I am adding to the queue and when I am checking the queue. – user2195430 Aug 11 '15 at 14:01
  • @user2195430 thanks for the update - I'm glad it worked out for you. – Michael Vincent Aug 11 '15 at 15:11
  • Also implemented this method. Makes the most sense, small change, safe and works great. – Luke Dupin Aug 14 '19 at 05:06