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
    #27

    @JonB said in Get content of cell from QTableView:

    self.dlg.tableView_3.selectionModel().currentIndex().row()

    Well, It works, but I need to separate it to put every attribute to INSER INTO sql query.

    Thnaks for help!

    JonBJ 1 Reply Last reply
    0
    • Karoluss96K Karoluss96

      @JonB said in Get content of cell from QTableView:

      self.dlg.tableView_3.selectionModel().currentIndex().row()

      Well, It works, but I need to separate it to put every attribute to INSER INTO sql query.

      Thnaks for help!

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

      @Karoluss96
      I don't know why you quote the first line of this code, but anyway.

      Well, It works, but I need to separate it to put every attribute to INSER INTO sql query.

      And what is the problem with that? Assuming

      rowData = [ model.index(row, col).data() for col in range(model.columnCount()) ]
      

      works in Python you have rowData containing all the values you need for your SQL statement.

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

        I suppose true. I forgot to change the first word in my previous answer from nagative to positive :-)

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

          If can add something for it:
          I need to change all empty (if is empty!) records in rowData from 'None' Type to 'Null' (for better recognising it in SQL Question).

          It's obviosly rather a python than qt question, but I can't find good answer in a differnt forum.

          JonBJ 1 Reply Last reply
          0
          • Karoluss96K Karoluss96

            If can add something for it:
            I need to change all empty (if is empty!) records in rowData from 'None' Type to 'Null' (for better recognising it in SQL Question).

            It's obviosly rather a python than qt question, but I can't find good answer in a differnt forum.

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

            @Karoluss96
            Sorry but this question does not mean much. If you are, say, generating an INSERT statement from the column values in a row, and some of these are Python None, and you need to pass NULL for this to SQL in VALUES clause, then you must write code to do so at the appropriate point. If you use, say, QSqlQuery to generate the statement you may find that QSqlQuery::addBindValue() does this for you if the value is Python None, I don't know.

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

              @JonB said in Get content of cell from QTableView:

              addBindValue()

              well first I want to concatenate data from rowData into one 'train' of later sql Values like below:

              dn2 = iden + ',\'' + teryt + '\',\'' +....+ DatMod
                           
              

              where iden is rowCount[0], teryt is rowCount[1] etc.

              but it take erorr:

              TypeError: can only concatenate str (not "NoneType") to str

              jsulmJ JonBJ 2 Replies Last reply
              0
              • Karoluss96K Karoluss96

                @JonB said in Get content of cell from QTableView:

                addBindValue()

                well first I want to concatenate data from rowData into one 'train' of later sql Values like below:

                dn2 = iden + ',\'' + teryt + '\',\'' +....+ DatMod
                             
                

                where iden is rowCount[0], teryt is rowCount[1] etc.

                but it take erorr:

                TypeError: can only concatenate str (not "NoneType") to str

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #33

                @Karoluss96 said in Get content of cell from QTableView:

                TypeError: can only concatenate str (not "NoneType") to str

                @JonB told you that you will have to write code for that. You need to check whether it is None, if it is add "NULL" instead of the value into the string.

                But it would be way better to use QSqlQuery::addBindValue() anyway...

                Karoluss96K 1 Reply Last reply
                1
                • Karoluss96K Karoluss96

                  @JonB said in Get content of cell from QTableView:

                  addBindValue()

                  well first I want to concatenate data from rowData into one 'train' of later sql Values like below:

                  dn2 = iden + ',\'' + teryt + '\',\'' +....+ DatMod
                               
                  

                  where iden is rowCount[0], teryt is rowCount[1] etc.

                  but it take erorr:

                  TypeError: can only concatenate str (not "NoneType") to str

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

                  @Karoluss96
                  Both of what @jsulm said.

                  Since I already wrote

                  and some of these are Python None, and you need to pass NULL for this to SQL in VALUES clause, then you must write code to do so at the appropriate point.

                  I don't understand why you come back with TypeError: can only concatenate str (not "NoneType") to str error message when you haven't done anything about it. Please read the answers and act upon them, else it's pointless asking/responding.

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Karoluss96 said in Get content of cell from QTableView:

                    TypeError: can only concatenate str (not "NoneType") to str

                    @JonB told you that you will have to write code for that. You need to check whether it is None, if it is add "NULL" instead of the value into the string.

                    But it would be way better to use QSqlQuery::addBindValue() anyway...

                    Karoluss96K Offline
                    Karoluss96K Offline
                    Karoluss96
                    wrote on last edited by Karoluss96
                    #35

                    @jsulm I try inspiring from here: https://cpp.hotexamples.com/examples/-/QSqlQuery/addBindValue/cpp-qsqlquery-addbindvalue-method-examples.html but still I have only one

                    jsulmJ 1 Reply Last reply
                    0
                    • Karoluss96K Karoluss96

                      @jsulm I try inspiring from here: https://cpp.hotexamples.com/examples/-/QSqlQuery/addBindValue/cpp-qsqlquery-addbindvalue-method-examples.html but still I have only one

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #36

                      @Karoluss96 said in Get content of cell from QTableView:

                      but still I have only one

                      Only one what? Can you please be more precise?

                      Karoluss96K 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @Karoluss96 said in Get content of cell from QTableView:

                        but still I have only one

                        Only one what? Can you please be more precise?

                        Karoluss96K Offline
                        Karoluss96K Offline
                        Karoluss96
                        wrote on last edited by
                        #37

                        @jsulm I mean if I try:

                         a=query.addBindValue(rowData)
                                    print (a)
                        

                        still have one 'None'

                        jsulmJ 2 Replies Last reply
                        0
                        • Karoluss96K Karoluss96

                          @jsulm I mean if I try:

                           a=query.addBindValue(rowData)
                                      print (a)
                          

                          still have one 'None'

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #38
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • Karoluss96K Karoluss96

                            @jsulm I mean if I try:

                             a=query.addBindValue(rowData)
                                        print (a)
                            

                            still have one 'None'

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #39

                            @Karoluss96 How does the SQL query look like? What data does rowData contain? Do you have more than one place holder in your querry to fill?

                            1 Reply Last reply
                            1
                            • Karoluss96K Offline
                              Karoluss96K Offline
                              Karoluss96
                              wrote on last edited by
                              #40
                              sql = ' INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,ZAM,...) VALUES ( '       
                                   
                              

                              after rowData:

                              query = QSqlQuery(db)  
                               dane2 =  iden + ',\'' + teryt + '\',\'' +zam + '\',\'' +... + DatMod  
                                         print (dane2)
                                         sqlCale = sql + dane2 + ')'
                                         print (sqlCale)
                                         query.exec_(sqlCale)
                                         while query.next():
                                             self.model4.insertRow(row)
                              

                              Insert Row isn't finished yet

                              Only in values data are filling

                              jsulmJ 1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #41

                                What exactly are expecting this code to do ?
                                Also, as already written by my fellows, you should rather use bind values to build your final query rather than building the string by hand.
                                Also, you are not doing any error checks with regard to the query execution and finally, your code seems to do two different counter intuitive things. First you do a database insertion and next you seem to try to retrieve data as a result of that insertion query.

                                So what is you exact goal ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                                  well.. when I put last code in this topic I found that I forgot add ')' between values and word 'values' :-P

                                  The only one thing to do is change default (non)value python's None to sql's NULL

                                  1 Reply Last reply
                                  0
                                  • Karoluss96K Karoluss96
                                    sql = ' INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,ZAM,...) VALUES ( '       
                                         
                                    

                                    after rowData:

                                    query = QSqlQuery(db)  
                                     dane2 =  iden + ',\'' + teryt + '\',\'' +zam + '\',\'' +... + DatMod  
                                               print (dane2)
                                               sqlCale = sql + dane2 + ')'
                                               print (sqlCale)
                                               query.exec_(sqlCale)
                                               while query.next():
                                                   self.model4.insertRow(row)
                                    

                                    Insert Row isn't finished yet

                                    Only in values data are filling

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #43

                                    @Karoluss96 I'm really confused now: we were talking about using addBindValue and you post now code which does not use it at all - WHY? Why don't you use addBindValue?!

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

                                      For a few moments I did sth different with this code outside of this topic.

                                      Now I try to use addBindValue, but I'm sure wher to put this function

                                      addBindValue is in for loop:

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

                                        For a few moments I did sth different with this code outside of this topic.

                                        Now I try to use addBindValue, but I'm sure wher to put this function

                                        addBindValue is in for loop:

                                          rowData = [ model.index(row, col).data() for col in range(model.columnCount()) ]
                                                    a=query.addBindValue(rowData)
                                                    print (a)
                                                    iden = rowData[0]
                                        ...
                                        
                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by JonB
                                        #45

                                        @Karoluss96 said in Get content of cell from QTableView:

                                        Now I try to use addBindValue, but I'm sure wher to put this function

                                        Then you should take the time to search the QSqlQuery doc page for bindValue, where you would come across the examples in subtopic Approaches to Binding Values. It is a really good idea to search the docs for your question before posting to say you do not know how to do something.

                                          rowData = [ model.index(row, col).data() for col in range(model.columnCount()) ]
                                                    a=query.addBindValue(rowData)
                                        

                                        No, there is no loop where you say there is a loop in your code. You cannot just add the whole rowData list in a single addBindValue, you need to add a bound value for each placeholder in the INSERT statement.

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

                                          the whole loop looks that:

                                          for col in range(model.columnCount()):
                                                      row1=model.index(row, col).data()
                                                      #print (row1)
                                                      rowData = [ model.index(row, col).data() for col in range(model.columnCount()) ]
                                                      a=query.addBindValue(rowData)
                                                      print (a)
                                                      iden = rowData[0]
                                                      teryt = rowData[1]
                                          ...
                                          

                                          So As I understand I have to move addBind Value for

                                          eg. ?

                                            iden =query.addBindValue( rowData[0])
                                          
                                          jsulmJ JonBJ 2 Replies 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