Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Changes various values in self.model (in TableView) to gain dat to sql query
Forum Updated to NodeBB v4.3 + New Features

Changes various values in self.model (in TableView) to gain dat to sql query

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 242 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Karoluss96
    wrote on 25 Aug 2023, 07:18 last edited by
    #1

    Good morning,

    The main problem for me now is (quote from Chef's list):

    "It's a pity that when editing values in many records, you have to enter each value by pressing "tick". When completing records, this is direct speed. Isn't it possible to participate to save only after all changes.[?]”

    tick is:

    self.dlg.pushButton_18.clicked.connect(self.prwn_zam) #button that trigger function.
    

    What I had earlier was that: only one record had been selected so that the data in self.model4 would run to the SQL query (it has more than 30 records, so only fragments will be shown), but the user, by the "tick" could be used only once - after all changes to self.model4.

    Control chief preseted:

    self.model4.dataChanged.connect(self.test)
    

    in the plug-in function
    then a table was created in the test function

    changedRowsTableView_3 = []
    

    and then used for:

    i = self.dlg.tableView_3.selectionModel().currentIndex().row()#a table where data was stored
    if not and in changedRowsTableView_3:
            changedRowsTableView_3.append(i)
             print('Line changed',i)# showed the line number
    

    I've now moved it all to prwn_zam, but I'm not sure if it's right.

    After many tries I now have this:

    def prwn_zam(self): #confirm changes to order records
             print('Approve button clicked!') #ticked pushButton_18
             con= self.model4.dataChanged.connect(self.prwn_test)
             if con:
                 print("Connection has been established.")
             else:
                 print("Failed to create connection.")
          
          
         def prwn_test(self):
             changedRowsTableView_3 = []
             record_count = self.model4.rowCount()
             i = self.dlg.tableView_3.selectionModel().currentIndex().row()
             idi = self.dlg.tableView_3.selectionModel().currentIndex().siblingAtRow(i).siblingAtColumn(33).data()
    # fetches the data from the row which is then queryed
             country = self.dlg.tableView_3.selectionModel().currentIndex().siblingAtRow(i).siblingAtColumn(0).data()
             ter = self.dlg.tableView_3.selectionModel().currentIndex().siblingAtRow(i).siblingAtColumn(1).data()
             act = self.dlg.tableView_3.selectionModel().currentIndex().siblingAtRow(i).siblingAtColumn(2).data()
    #...
              if not and in  changedRowsTableView_3 :
                      changedRowsTableView_3 .append(i)
                     print('Changed row ',i, ' with ID: ', idi, ter, act)
                     if idi is not None:
                            idi = int(float(idi))
             danRek.append(idi)
             danRek1.append(ter)
             danRek2.append(act)
    #danRek[1-32] are tables created earlier. I also have doubts whether they should be created in this function or in the startup plugin?
    

    Then there's my part:

      for j, k, l, m, n, o .... in zip(danRek, danRek1, danRek2, danRek3, danRek4, danRek5, ...):
                         if isinstance(k, str) and k != 'NULL': #teryt
                             k ='\'' + k + '\''
                         if isinstance(n, str) and n != 'NULL': #Orderer
                             n ='\'' + n + '\''
                           sql = f"UPDATE ORDER.MANAGEMENT SET teryt={k}, ORDERING_CODE={n}, ..., DATA_MOD_REC=TO_DATE(SYSDATE,\'DD.MM.YYYY HH24:MI:SS\') WHERE ID={ j}"
                         print (sql)
                         sql = query.prepare(sql)
                         if query.exec():
                             print('added!')
                         else:
                             print(query.lastError().text())
    

    which is already working - the query works fine, but the data that goes to it aren't correct, and it is to be executed in a loop as many times as many records have been changed. Not only once by selected record (I have already removed the old code, but I have it in archived - previous version of the code).

    I'm sure that I have a problem with the location n of this code- in which function (before the last fragment).

    Thank you in advance for every kind of help!

    J 1 Reply Last reply 25 Aug 2023, 08:05
    0
    • K Karoluss96
      25 Aug 2023, 07:18

      Good morning,

      The main problem for me now is (quote from Chef's list):

      "It's a pity that when editing values in many records, you have to enter each value by pressing "tick". When completing records, this is direct speed. Isn't it possible to participate to save only after all changes.[?]”

      tick is:

      self.dlg.pushButton_18.clicked.connect(self.prwn_zam) #button that trigger function.
      

      What I had earlier was that: only one record had been selected so that the data in self.model4 would run to the SQL query (it has more than 30 records, so only fragments will be shown), but the user, by the "tick" could be used only once - after all changes to self.model4.

      Control chief preseted:

      self.model4.dataChanged.connect(self.test)
      

      in the plug-in function
      then a table was created in the test function

      changedRowsTableView_3 = []
      

      and then used for:

      i = self.dlg.tableView_3.selectionModel().currentIndex().row()#a table where data was stored
      if not and in changedRowsTableView_3:
              changedRowsTableView_3.append(i)
               print('Line changed',i)# showed the line number
      

      I've now moved it all to prwn_zam, but I'm not sure if it's right.

      After many tries I now have this:

      def prwn_zam(self): #confirm changes to order records
               print('Approve button clicked!') #ticked pushButton_18
               con= self.model4.dataChanged.connect(self.prwn_test)
               if con:
                   print("Connection has been established.")
               else:
                   print("Failed to create connection.")
            
            
           def prwn_test(self):
               changedRowsTableView_3 = []
               record_count = self.model4.rowCount()
               i = self.dlg.tableView_3.selectionModel().currentIndex().row()
               idi = self.dlg.tableView_3.selectionModel().currentIndex().siblingAtRow(i).siblingAtColumn(33).data()
      # fetches the data from the row which is then queryed
               country = self.dlg.tableView_3.selectionModel().currentIndex().siblingAtRow(i).siblingAtColumn(0).data()
               ter = self.dlg.tableView_3.selectionModel().currentIndex().siblingAtRow(i).siblingAtColumn(1).data()
               act = self.dlg.tableView_3.selectionModel().currentIndex().siblingAtRow(i).siblingAtColumn(2).data()
      #...
                if not and in  changedRowsTableView_3 :
                        changedRowsTableView_3 .append(i)
                       print('Changed row ',i, ' with ID: ', idi, ter, act)
                       if idi is not None:
                              idi = int(float(idi))
               danRek.append(idi)
               danRek1.append(ter)
               danRek2.append(act)
      #danRek[1-32] are tables created earlier. I also have doubts whether they should be created in this function or in the startup plugin?
      

      Then there's my part:

        for j, k, l, m, n, o .... in zip(danRek, danRek1, danRek2, danRek3, danRek4, danRek5, ...):
                           if isinstance(k, str) and k != 'NULL': #teryt
                               k ='\'' + k + '\''
                           if isinstance(n, str) and n != 'NULL': #Orderer
                               n ='\'' + n + '\''
                             sql = f"UPDATE ORDER.MANAGEMENT SET teryt={k}, ORDERING_CODE={n}, ..., DATA_MOD_REC=TO_DATE(SYSDATE,\'DD.MM.YYYY HH24:MI:SS\') WHERE ID={ j}"
                           print (sql)
                           sql = query.prepare(sql)
                           if query.exec():
                               print('added!')
                           else:
                               print(query.lastError().text())
      

      which is already working - the query works fine, but the data that goes to it aren't correct, and it is to be executed in a loop as many times as many records have been changed. Not only once by selected record (I have already removed the old code, but I have it in archived - previous version of the code).

      I'm sure that I have a problem with the location n of this code- in which function (before the last fragment).

      Thank you in advance for every kind of help!

      J Offline
      J Offline
      JonB
      wrote on 25 Aug 2023, 08:05 last edited by
      #2

      @Karoluss96 said in Changes various values in self.model (in TableView) to gain dat to sql query:

      Isn't it possible to participate to save only after all changes.[?]”

      Consider changing over to use QSqlTableModel Class. A QTableView can use that as its model. It allows you to "buffer" changes in the client program until you are ready to QSqlTableModel::submitAll() to submit all the changes in one go.

      K 1 Reply Last reply 25 Aug 2023, 08:56
      2
      • J JonB
        25 Aug 2023, 08:05

        @Karoluss96 said in Changes various values in self.model (in TableView) to gain dat to sql query:

        Isn't it possible to participate to save only after all changes.[?]”

        Consider changing over to use QSqlTableModel Class. A QTableView can use that as its model. It allows you to "buffer" changes in the client program until you are ready to QSqlTableModel::submitAll() to submit all the changes in one go.

        K Offline
        K Offline
        Karoluss96
        wrote on 25 Aug 2023, 08:56 last edited by
        #3

        @JonB Look intersting I'll respond if I still next problem appears.

        1 Reply Last reply
        0

        1/3

        25 Aug 2023, 07:18

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved