We are trying to write concurrently to a SQLite database, using WAL mode, to a network location. We don't have the privileges of having a server-based database (Postgres, MySQL, etc) so we have to utilise SQLite only unfortunately. The basic application we're developing needs to make use of the SQLite database and the user has the values calculated on their UI (using PyQT5) which then needs to be written back to the SQLite database.
When one person is using the program, it writes to the database with no issues. However, when multiple users try to write their output to the database via the code, there seems to be an issue which we can't resolve and it's this we would like to get any solutions on if any others have encountered it.
What we're finding is the user who gets their inputs to run last on their UI, has their figures updated to the database (two files, a -wal file and a -shm file are created) but one of the other user's data doesn't get recorded to the database, even though they can see it on their UI.
First, here is the SQL code to create the database and enter data:
CREATE TABLE Employee (
"Name" TEXT,
"Age" INTEGER,
"City" TEXT,
"Bonus" REAL,
"Department" TEXT);
INSERT INTO Employee (Name, Age, City, Bonus, Department) VALUES ('Sam', 30, 'DL', NULL, 'Sales');
INSERT INTO Employee (Name, Age, City, Bonus, Department) VALUES ('James', 40, 'NC', NULL, 'IT');
INSERT INTO Employee (Name, Age, City, Bonus, Department) VALUES ('Craig', 50, 'LD', NULL, 'Finance');
INSERT INTO Employee (Name, Age, City, Bonus, Department) VALUES ('John', 60, 'MC', NULL, 'HR');
INSERT INTO Employee (Name, Age, City, Bonus, Department) VALUES ('Tracy', 48, 'DH', NULL, 'Customer Support');
INSERT INTO Employee (Name, Age, City, Bonus, Department) VALUES ('Sarah', 39, 'LP', NULL, 'Marketing');
Below, is the code for the program:
import sys
from PyQt5.QtSql import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class TableViewer(QMainWindow):
def __init__(self):
super().__init__()
#createDB()
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('sample.db')
# try to open the database
if not self.db.open():
raise QSqlError("Could not open the database")
# this will give you the whole table:
self.model = QSqlTableModel(self, self.db)
self.model.setTable('Employee')
self.model.select()
self.tview = QTableView(self)
self.tview.setModel(self.model)
h_header = self.tview.horizontalHeader()
h_header.setSectionResizeMode(QHeaderView.Stretch)
layout = QVBoxLayout()
layout.addWidget(self.tview)
widget = QWidget()
widget.setLayout(layout)
widget.setFixedHeight(350)
self.setCentralWidget(widget)
self.copy_btn = QPushButton()
self.copy_btn.setText("Calculate")
layout.addWidget(self.copy_btn, alignment=Qt.AlignAbsolute)
self.copy_btn.clicked.connect(self.Calculate)
self.show()
def Calculate(self):
for i in range(self.model.rowCount()):
record = self.model.record(i)
department = record.value("Department")
if department == 'Sales':
record.setValue("Bonus", 1500)
if department == 'IT':
record.setValue("Bonus", 3000)
if department == 'Finance':
record.setValue("Bonus", 2000)
self.model.setRecord(i, record)
self.model.submitAll()
if __name__ == '__main__':
app = QApplication(sys.argv)
tv = TableViewer()
tv.showMaximized()
sys.exit(app.exec_())
To replicate the issue we're having, the other user would be trying to enter the following in their Calculate function:
def Calculate(self):
for i in range(self.model.rowCount()):
record = self.model.record(i)
department = record.value("Department")
if department == 'HR':
record.setValue("Bonus", 2500)
if department == 'Customer Support':
record.setValue("Bonus", 1000)
if department == 'Marketing':
record.setValue("Bonus", 500)
self.model.setRecord(i, record)
self.model.submitAll()
The SQLite database has the following settings:
Any help to resolve this would be greatly appreciated. Thanks in advance!
