I’m using QSqlTableModel connected to sqlite3 database. My connection works fine and also my data are properly set: when I insert a record, after starting the program, everything work fine, but when I try to insert a new record, I receive the error “Parameter count mismatch” because the following code goes in error
if not self.table_model.submitAll():
error_message = f"Error Customer Model: {self.table_model.lastError().text()}"
raise CustomModelError(error_message)
that’s the self.table_model.submitAll() fails.
the piece of code is
def update_customer(self, customer_id, data):
try:
self.db_model.open_connection()
row = self.find_row_by_customer_id(customer_id)
record = self.set_db_record(data, row)
self.table_model.setRecord(row, record)
self.confirm_database_changes()
return customer_id
finally:
self.db_model.close_connection()
def find_row_by_customer_id(self, customer_id):
row = -1
for i in range(self.table_model.rowCount()):
if str(self.table_model.data(self.table_model.index(i, 0))) == str(customer_id):
row = i
break
return row
def confirm_database_changes(self):
if not self.table_model.submitAll():
error_message = f"Error Customer Model: {self.table_model.lastError().text()}"
raise CustomModelError(error_message)
self.table_model.database().commit()
self.table_model.select()
self.select()
as you can see in confirm_database_changes method, I refresh the model after inserting the new record, but the error still exists.
Any help is higlhy appreciated.
Thanks.
Please provide a more comprehensive minimal reproducible example, most importantly
set_db_record()
. Also, check the return value ofsetRecord()
, which is probablyFalse
in your case.It’s somewhat bizarre that you choose to iterate over the rows to find the id. Why not just query the db? It also looks totally broken that
find_row_by_customer_id
returns-1
on failure rather than raising an error. Why would you want to pass a known invalid id to those other methods?