skip to Main Content

I have following idea: user can edit database and when he will press exit from database he will be asked if he want to save edited database or if he edited something and then turned it back he wont be asked.

I think i should compare created database and database after editing when user press exit, but don’t know how.

This is my code for creating database

    model = new QSqlRelationalTableModel(this, *db);
    model->setTable("cv");
    model->setFilter("cv_id = "+currentCV+"");
    model->removeColumns(0,1);
    model->select();
    ui->tableView->show();
    ui->tableView->setModel(model);

2

Answers


  1. You have 2 options.

    1. Create event triggers in the database. Event triggers work when users change database table structures or create tables or change columns. Thus, these triggers work when the user executes any DDL commands. (change table, add column, create index, drop table, etc.) You can insert these commands into your log tables using event triggers.

    2. Your database structures (all tables, columns, indexes, etc.) are stored in information_shema. You can select the data of these tables and save it somewhere and then compare it with the data that has changed.

    Login or Signup to reply.
  2. Take in mind that no change to database is performed before you’re submitting the user changes. By default, sumbit is performed when changes occur, and this does not help you. But, you can set

    model.setEditStrategy(QSqlTableModel.EditStrategy.OnManualSubmit)
    

    At this point, changes are persisted only when the method model.submitAll() is called.

    All you have to do, at this point, is exploiting dataChanged signal of your model and use a flag variable to check if changes have been operated:

    data_changed = False
    
    def on_data_changed()
        data_changed = True
    
    model.dataChanged.connect(on_data_changed)
    

    Now you can adjust the logic of the code to serve your specific purpose

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search