Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get content of cell from QTableView
Forum Updated to NodeBB v4.3 + New Features

Get content of cell from QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
88 Posts 7 Posters 57.3k Views 1 Watching
  • 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.
  • Karoluss96K Offline
    Karoluss96K Offline
    Karoluss96
    wrote on last edited by
    #61

    It's a rest from previous version.

    I'll continue that tommorow as I forgot about more importat thing: coverting date formats!
    (I'm sure that I solve on my own)

    1 Reply Last reply
    0
    • Karoluss96K Offline
      Karoluss96K Offline
      Karoluss96
      wrote on last edited by
      #62

      Still I have problem

      There's no error, but on console I see only none.

      Current code looks that:

       sql = query.prepare('INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,ZAM....)'
                                  'VALUES (?,?,?....)')       
       index =self.dlg.tableView_3.selectionModel().currentIndex()
       dane= self.dlg.tableView_3.model().index(index.row(),0).data() 
       row = index.row()
              model = self.dlg.tableView_3.model()
              for col in range(model.columnCount()):
                  rowData = [ model.index(row, col).data() for col in range(model.columnCount()) ]
                  if (rowData[0] is None):
                      iden =query.bindValue(rowData[0], '')
                      print (iden)
                  else:
                      iden = query.bindValue(0,rowData[0])
           
                      print (iden)
                  if (rowData[1] is None):    
                      teryt = query.bindValue(rowData[1],'')
                      print (teryt)
                  else:
                      teryt = query.bindValue(1,rowData[1])
                      print (teryt)
        print (sql)
        y=query.exec_()
        print (y)
      ...
      
      sql take True, but "y" False
      JonBJ 1 Reply Last reply
      0
      • Karoluss96K Karoluss96

        Still I have problem

        There's no error, but on console I see only none.

        Current code looks that:

         sql = query.prepare('INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,ZAM....)'
                                    'VALUES (?,?,?....)')       
         index =self.dlg.tableView_3.selectionModel().currentIndex()
         dane= self.dlg.tableView_3.model().index(index.row(),0).data() 
         row = index.row()
                model = self.dlg.tableView_3.model()
                for col in range(model.columnCount()):
                    rowData = [ model.index(row, col).data() for col in range(model.columnCount()) ]
                    if (rowData[0] is None):
                        iden =query.bindValue(rowData[0], '')
                        print (iden)
                    else:
                        iden = query.bindValue(0,rowData[0])
             
                        print (iden)
                    if (rowData[1] is None):    
                        teryt = query.bindValue(rowData[1],'')
                        print (teryt)
                    else:
                        teryt = query.bindValue(1,rowData[1])
                        print (teryt)
          print (sql)
          y=query.exec_()
          print (y)
        ...
        
        sql take True, but "y" False
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #63

        @Karoluss96

        1. Your code approach makes little sense. Why do you have any for col in range(model.columnCount()): loop at all, given that you do not access col, and you do all binding explicitly for each column by number in the body?

        2. If your code is really as shown you have 3+ values to bind but you only access rowdata elements 0 & 1.

        3. You have query.bindValue(0,rowData[0]), which is correct, but for the if (rowData[0] is None) case you have put bindValue(rowData[0], ''). You can see that is a quite different overload, why have chosen that? You can see that evaluates to bindValue(None, ''), which you can tell is not going to be helpful.

        4. When query.exec_() returns False you should access QSqlError QSqlQuery::lastError() const to discover information about what went wrong, which may tell you why it is not working.

        1 Reply Last reply
        1
        • Karoluss96K Offline
          Karoluss96K Offline
          Karoluss96
          wrote on last edited by Karoluss96
          #64

          Ok, so first:

          Are you suggeest to change (e.g.):

          bindValue(rowData[7],'')# here's 33 attributes and values so that I cut fo this forum to first 3
          

          to

          bindValue(None, ' ')
          

          Bofore this example I've tried with (e.g.)

          addBindValue(rowData[19])
          

          but It take the same result

          query.lastError() gains: <PyQt5.QtSql.QSqlError object at 0x000002A469F43AC0>

          for col in range(model.columnCount()):
          

          was presented to me in this disscution some threads above

          JonBJ 1 Reply Last reply
          0
          • Karoluss96K Karoluss96

            Ok, so first:

            Are you suggeest to change (e.g.):

            bindValue(rowData[7],'')# here's 33 attributes and values so that I cut fo this forum to first 3
            

            to

            bindValue(None, ' ')
            

            Bofore this example I've tried with (e.g.)

            addBindValue(rowData[19])
            

            but It take the same result

            query.lastError() gains: <PyQt5.QtSql.QSqlError object at 0x000002A469F43AC0>

            for col in range(model.columnCount()):
            

            was presented to me in this disscution some threads above

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #65

            @Karoluss96 said in Get content of cell from QTableView:

            Are you suggeest to change (e.g.):

            No I am not! I am pointing out what your code does, and inviting you to realize that it cannot be correct.

            Please stop, look at your code and think about it:

                        if (rowData[0] is None):
                            iden =query.bindValue(rowData[0], '')
                            print (iden)
                        else:
                            iden = query.bindValue(0,rowData[0])
            

            When rowData[0] is not None this binds positional parameter #0 (the first ?) to the value of rowData[0], which is reasonable. What happens when rowData[0] is None? It uses a quite different overload of QSqlQuery::bindValue() --- why? And since rowData[0] is None it will evaluate to query.bindValue(None, '') --- what is that supposed to achieve?

            I suggest you start by getting your code working when none of the elements in rowData is None. I don't care how you achieve that, just make sure rowData does not contain any Nones, it's just a test. Is the INSERT statement now fully working? If & when it is, time to address None --- discover what value you have to bind in order for it to be acceptable and result in NULL in the final INSERT. I don't know what value that is from Python, only from C++. Maybe None works, maybe QVariant() works, I don't know. Come back here when it's all working other than for None values if you can't figure it.

            query.lastError() gains: <PyQt5.QtSql.QSqlError object at 0x000002A469F43AC0>

            You are supposed to figure out what to print from a QSqlError that is a human-readable description of the error, not just blindly put in print(query.lastError()) which gives the above.

            for col in range(model.columnCount()):

            was presented to me in this disscution some threads above

            Just because something is "presented" you don't just put it into your code automatically. You are supposed to understand what you do and don't need a loop for.

            1 Reply Last reply
            1
            • Karoluss96K Offline
              Karoluss96K Offline
              Karoluss96
              wrote on last edited by Karoluss96
              #66
              rowData = [model.index(row, col).data() for col in range(model.columnCount()) ]
              

              is compulsory as the values for sql query come from Table View, where the user put by own!

              Then, you suggest that:

              query.bindValue(None, '')
              

              is much better than with rowData[0,1,2....33]!?

              To check the full values is difficult, because there a lot of in date format, about which I mentioned her I also have probles to convert it correctly.
              Now here's:

              query.addBindValue(datetime.strptime(rowData[15],'%d.%m.%Y')
              

              During the fights with this code one version went good (query.exec_() get true), but It's diffuclt to reconstruct it

              JonBJ 1 Reply Last reply
              0
              • Karoluss96K Karoluss96
                rowData = [model.index(row, col).data() for col in range(model.columnCount()) ]
                

                is compulsory as the values for sql query come from Table View, where the user put by own!

                Then, you suggest that:

                query.bindValue(None, '')
                

                is much better than with rowData[0,1,2....33]!?

                To check the full values is difficult, because there a lot of in date format, about which I mentioned her I also have probles to convert it correctly.
                Now here's:

                query.addBindValue(datetime.strptime(rowData[15],'%d.%m.%Y')
                

                During the fights with this code one version went good (query.exec_() get true), but It's diffuclt to reconstruct it

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #67

                @Karoluss96 said in Get content of cell from QTableView:

                Then, you suggest that:
                query.bindValue(None, '')

                is much better than with rowData[0,1,2....33]!?

                Did you actually read my response? I have now said twice

                Are you suggeest to change (e.g.):

                No I am not! I am pointing out what your code does, and inviting you to realize that it cannot be correct.

                How can you keep saying I suggest query.bindValue(None, '') when I have said that I do not suggest it and it is wrong?

                I have asked you to think about what you have. You don't seem to have done so. I have asked you what overload of QSqlQuery.bindValue() you are trying to use in query.bindValue(rowData[0], '') when the other (non-None, correct) one is query.bindValue(0,rowData[0]), and why you are trying to use a different overload. You don't seem to have done so. I have suggested you get it working fully when no value is None, yet you are still talking about the None case. I have said you need to print out the error message from the QSqlError object.

                If you want help please act on the responses you are given. And please think about your code, people here don't write code for you, they help you with questions about the code you try yourself after considering the problem, analyzing it and acting on the responses you are given. We are trying to help you to help yourself, not write code for you. You need to understand, not just copy/paste other people's stuff, else you will always be stuck on the next thing....

                1 Reply Last reply
                0
                • Karoluss96K Offline
                  Karoluss96K Offline
                  Karoluss96
                  wrote on last edited by
                  #68

                  It takes error: ORA-01722 (uncorrect number)

                  JonBJ 2 Replies Last reply
                  0
                  • Karoluss96K Karoluss96

                    It takes error: ORA-01722 (uncorrect number)

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #69

                    @Karoluss96
                    What takes that error? Are we supposed to guess? Oh, is that "it" supposed to be the QSqlError?? So you can Google for that yourself. Presumably (at least one of) your values are wrong.

                    1 Reply Last reply
                    0
                    • Karoluss96K Karoluss96

                      It takes error: ORA-01722 (uncorrect number)

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #70

                      @Karoluss96
                      I suggest you try (something like, untested):

                      sql = query.prepare('INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,ZAM....)'
                                                  'VALUES (?,?,?....)')       
                      index = self.dlg.tableView_3.selectionModel().currentIndex()
                      row = index.row()
                      model = self.dlg.tableView_3.model()
                      for col in range(model.columnCount()):
                          val = model.index(row, col).data()
                          if val is None:
                              raise Exception("Do not try to do a value of `None` yet, get it working for non-`None` values first!!!")
                          # print("Binding parameter number", col, "to value", str(val))
                          query.bindValue(col, val)  
                      if query.exec_():
                          print("Worked!")
                      else
                          print(query.lastError().text())
                      
                      1 Reply Last reply
                      0
                      • Karoluss96K Offline
                        Karoluss96K Offline
                        Karoluss96
                        wrote on last edited by Karoluss96
                        #71

                        From Beginning:

                        My first version of the code to make sql query worked!

                        I need to change it as there 2 problems:

                        • Date formats (cannot be concatenated with string values, like it was:
                        dn = iden + ',\'' + teryt + '\',\'' +zam + '\',\'+ ...
                        

                        )

                        • User will be put to 13th value (the last which is compulsory to make valid query). Rest 20 only If (s)he need, and (s)he don't want to use Tabulation (Tab key) to go through the whole table, which automatically changing None to ' '.

                        So that Now I testify the scenerio where the user put first 13th values and don't watch for next ones (so stays None), but sql need to change to NULL.
                        If this problem will be solve I'll go to data format, as the first date format is on 16th place of value (so in non-compulsory).

                        Is my point of view (and thinking) clear and understand?

                        JonBJ 1 Reply Last reply
                        0
                        • Karoluss96K Karoluss96

                          From Beginning:

                          My first version of the code to make sql query worked!

                          I need to change it as there 2 problems:

                          • Date formats (cannot be concatenated with string values, like it was:
                          dn = iden + ',\'' + teryt + '\',\'' +zam + '\',\'+ ...
                          

                          )

                          • User will be put to 13th value (the last which is compulsory to make valid query). Rest 20 only If (s)he need, and (s)he don't want to use Tabulation (Tab key) to go through the whole table, which automatically changing None to ' '.

                          So that Now I testify the scenerio where the user put first 13th values and don't watch for next ones (so stays None), but sql need to change to NULL.
                          If this problem will be solve I'll go to data format, as the first date format is on 16th place of value (so in non-compulsory).

                          Is my point of view (and thinking) clear and understand?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #72

                          @Karoluss96 said in Get content of cell from QTableView:

                          dn = iden + ',\'' + teryt + '\',\'' +zam + '\',\'+ ...

                          1. We have no idea what is in variables iden, teryt, zam, ....
                          2. What sort of string are you expecting this to produce (if it worked)? Give an example so we know!
                          3. Where are you trying to use this dn variable?

                          If you want a response, answer all 3 questions....

                          OK, hang on....
                          So now I see iden, teryt, zam, ... are assigned from iden =query.bindValue(rowData[0], '') etc. So what does QSqlQuery.bindValue() return as its result, look at the documentation, you tell me? When you discover that method does not return any value (no RETURN TYPE documented) you will realize what the problem is.....

                          1 Reply Last reply
                          0
                          • Karoluss96K Offline
                            Karoluss96K Offline
                            Karoluss96
                            wrote on last edited by
                            #73
                            1. iden, teryt, zam come from (slashes for better understand):
                            if (rowData[0] is None):
                                            iden/teryt/zam =query.bindValue(rowData[0/1/2], '')
                                            print (iden)
                                        else:
                                            iden/teryt/zam = query.addBindValue(rowData[0/1/2])# firsty here was: iden= rowData[0]
                                            print (iden)
                            
                            1. possible result:
                            7150, '1401', 'UM14', ....
                            
                            1. dn was later to sql query
                            sql =INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,ZAM...) VALUES (
                             sqlFull = sql + dn + ')'
                            query.exec_(sqlFull)
                            
                            JonBJ 1 Reply Last reply
                            0
                            • Karoluss96K Karoluss96
                              1. iden, teryt, zam come from (slashes for better understand):
                              if (rowData[0] is None):
                                              iden/teryt/zam =query.bindValue(rowData[0/1/2], '')
                                              print (iden)
                                          else:
                                              iden/teryt/zam = query.addBindValue(rowData[0/1/2])# firsty here was: iden= rowData[0]
                                              print (iden)
                              
                              1. possible result:
                              7150, '1401', 'UM14', ....
                              
                              1. dn was later to sql query
                              sql =INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,ZAM...) VALUES (
                               sqlFull = sql + dn + ')'
                              query.exec_(sqlFull)
                              
                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #74

                              @Karoluss96
                              Your response crossed with the "OK, hang on....` amd subsequent I appended to my previous. Please read that. Your code makes no sense.

                              possible result:

                              7150, '1401', 'UM14', ....

                              Your code won't produce anything like the above.

                              sqlFull = sql + dn + ')'

                              query.exec_(sqlFull)

                              Don't know what you're trying to achieve here, but don't do it. It's concerning that, if I understand/guess right, you have not understood the basics about binding values and how they are used. Either understand bound variables and use them, or decide you want to produce your own literal string for the INSERT statement which you will ensure is correct and don't use bound variables, but don't try to mix them, which I think is what you are trying to do.

                              1 Reply Last reply
                              0
                              • Karoluss96K Offline
                                Karoluss96K Offline
                                Karoluss96
                                wrote on last edited by
                                #75

                                This what you sent to me works correct!

                                My point of view is that (little psuedo-code):

                                The user put some from 33 values.

                                Those attributes were puts values:
                                get as value to sql query

                                Those attributes weren't put any value:
                                change automatic the default python NoneType to sql's NULL and get later to sql query

                                JonBJ 1 Reply Last reply
                                0
                                • Karoluss96K Karoluss96

                                  This what you sent to me works correct!

                                  My point of view is that (little psuedo-code):

                                  The user put some from 33 values.

                                  Those attributes were puts values:
                                  get as value to sql query

                                  Those attributes weren't put any value:
                                  change automatic the default python NoneType to sql's NULL and get later to sql query

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #76

                                  @Karoluss96 said in Get content of cell from QTableView:

                                  This what you sent to me works correct!

                                  Do you mean the code block I wrote a couple of posts ago above? You should be able to get that working so long as you do not yet have any None values in your data, have you tested that, does it work fully so long as no None values? Please answer!

                                  change automatic the default python NoneType to sql's NULL

                                  The problem is that there is no "sql's NULL" in Python, so we don't yet know what to do about Nones to get what we need across to SQL.

                                  If and when you confirm all is working from my code with no Nones involved, I will have a think about None/NULL. But not until you confirm my code works without Nones!

                                  1 Reply Last reply
                                  0
                                  • Karoluss96K Offline
                                    Karoluss96K Offline
                                    Karoluss96
                                    wrote on last edited by Karoluss96
                                    #77

                                    @JonB said in Get content of cell from QTableView:

                                    Do you mean the code block I wrote a couple of posts ago above? You should be able to get that working so long as you do not yet have any None values in your data, have you tested that, does it work fully so long as no None values? Please answer!

                                    If I go through the all values (by tabs or putting blank space or one digit sign) code work without Exception!

                                    The problem is that there is no "sql's NULL" in Python, so we don't yet know what to do about Nones to get what we need across to SQL.
                                    If and when you confirm all is working from my code with no Nones involved, I will have a think about None/NULL. But not until you confirm my code works without Nones!

                                    That the main reason for all this discussion- how to auto-change None to NULL?!

                                    JonBJ 1 Reply Last reply
                                    0
                                    • Karoluss96K Karoluss96

                                      @JonB said in Get content of cell from QTableView:

                                      Do you mean the code block I wrote a couple of posts ago above? You should be able to get that working so long as you do not yet have any None values in your data, have you tested that, does it work fully so long as no None values? Please answer!

                                      If I go through the all values (by tabs or putting blank space or one digit sign) code work without Exception!

                                      The problem is that there is no "sql's NULL" in Python, so we don't yet know what to do about Nones to get what we need across to SQL.
                                      If and when you confirm all is working from my code with no Nones involved, I will have a think about None/NULL. But not until you confirm my code works without Nones!

                                      That the main reason for all this discussion- how to auto-change None to NULL?!

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by JonB
                                      #78

                                      @Karoluss96 said in Get content of cell from QTableView:

                                      That the main reason for all this discussion- how to auto-change None to NULL?!

                                      I can give you some suggestions to try. But first I know you are using Python but not which Python binding you are using for Qt. It could be any of: PySide2, PyQt5, PySide6, PyQt6. Which one?

                                      1 Reply Last reply
                                      0
                                      • Karoluss96K Offline
                                        Karoluss96K Offline
                                        Karoluss96
                                        wrote on last edited by
                                        #79

                                        I suppose PyQt5

                                        as at beginng appears a line:

                                        from PyQt5.QtCore import QSettings
                                        
                                        JonBJ 1 Reply Last reply
                                        0
                                        • Karoluss96K Karoluss96

                                          I suppose PyQt5

                                          as at beginng appears a line:

                                          from PyQt5.QtCore import QSettings
                                          
                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by
                                          #80

                                          @Karoluss96
                                          In place of the raise Exception(...) line try the following:

                                          1. val = None. If this works it would be simplest! It means that binding a None, does result in a NULL, so you can remove the special case completely.

                                          2. val = QVariant(). Try this next. You may find you need a from PyQt5.QtCore import QVariant statement at the head of the file near the other PyQt5 imports.

                                          If #2 does not work I would be interested to know if you get an error message anywhere (e.g. perhaps on the query.exec_()) and what the message says.

                                          1 Reply Last reply
                                          0

                                          • Login

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