Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSqlTableModel and lock a record



  • Hello,
    i am currently working on a mariadb database with simultaneous access management.

    I was wondering if it was possible to lock a record before editing it (or not) with a QSqlTableModel.

    Otherwise, currently I use the QSqlQuery of a QSqlQueryModel : "SELECT... FOR UPDATE".
    The problem is that a QSqlQueryModel is read only, so after an update, (a delete or even an insert) with the QSqlQuery, you have to completely redisplay the table and then reselect the adapted row.

    So with a QSqlTableModel is it possible to check that a record is not locked, and in this case, to lock and then unlock it, in a transaction obviously ?

    Thanks. Regards. Gilles



  • At the moment I don't think it's possible, it would require changing QSqlTableModelPrivate::exec.



  • Thank you for the link.
    In qsqltablemodel.h, 4 signals are declared :

    Q_SIGNALS:
    123 void primeInsert(int row, QSqlRecord &record);
    124
    125 void beforeInsert(QSqlRecord &record);
    126 void beforeUpdate(int row, QSqlRecord &record);
    127 void beforeDelete(int row);
    .... and there is a protected method
    139 void setQuery(const QSqlQuery &query);

    I wonder if there is any way to use them to solve my problem.

    Regards. Gilles


  • Lifetime Qt Champion

    Hi,

    What I would try is to make the SELECT for UPDATE query in a slot connected to the beforeUpdate. You need a direct connection (which is the default unless you are using objects in different threads).


Log in to reply