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.2k 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.
  • 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
                      • Karoluss96K Karoluss96

                        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 Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #47

                        @Karoluss96 said in Get content of cell from QTableView:

                        eg. ?
                        iden =query.addBindValue( rowData[0])

                        yes, for every value in your query you want to bind you need to call addBindValue as you can clearly see in the example:

                        QSqlQuery query;
                        query.prepare("INSERT INTO person (id, forename, surname) "
                                          "VALUES (?, ?, ?)");
                        query.addBindValue(1001);
                        query.addBindValue("Bart");
                        query.addBindValue("Simpson");
                        query.exec();
                        
                        1 Reply Last reply
                        1
                        • Karoluss96K Karoluss96

                          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])
                          
                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #48

                          @Karoluss96
                          Indeed, for each element in rowData in a loop. And you must make sure your INSERT query has the right number of column names and ? (or :<name> if you are using those) placeholders in the VALUES (...) clause to match the bound values added.

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

                            @jsulm said in Get content of cell from QTableView:

                            query.prepare(

                            Now I have this:

                            query = QSqlQuery(db)
                             sql = query.prepare('INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,...) 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()) ]
                                        iden =query.BindValue(rowData[0], '')
                                        print (iden)
                            ...
                            

                            with error:
                            AttributeError: 'QSqlQuery' object has no attribute 'BindValue'

                            db is a global value:

                            db = QSqlDatabase.addDatabase("QOCISPATIAL")
                            
                            jsulmJ JonBJ 3 Replies Last reply
                            0
                            • Karoluss96K Karoluss96

                              @jsulm said in Get content of cell from QTableView:

                              query.prepare(

                              Now I have this:

                              query = QSqlQuery(db)
                               sql = query.prepare('INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,...) 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()) ]
                                          iden =query.BindValue(rowData[0], '')
                                          print (iden)
                              ...
                              

                              with error:
                              AttributeError: 'QSqlQuery' object has no attribute 'BindValue'

                              db is a global value:

                              db = QSqlDatabase.addDatabase("QOCISPATIAL")
                              
                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #50

                              @Karoluss96 said in Get content of cell from QTableView:

                              AttributeError: 'QSqlQuery' object has no attribute 'BindValue'

                              Please , isn't this apparent?! Really...

                              1 Reply Last reply
                              0
                              • Karoluss96K Karoluss96

                                @jsulm said in Get content of cell from QTableView:

                                query.prepare(

                                Now I have this:

                                query = QSqlQuery(db)
                                 sql = query.prepare('INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,...) 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()) ]
                                            iden =query.BindValue(rowData[0], '')
                                            print (iden)
                                ...
                                

                                with error:
                                AttributeError: 'QSqlQuery' object has no attribute 'BindValue'

                                db is a global value:

                                db = QSqlDatabase.addDatabase("QOCISPATIAL")
                                
                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #51

                                @Karoluss96 said in Get content of cell from QTableView:

                                db is a global value:
                                db = QSqlDatabase.addDatabase("QOCISPATIAL")

                                There should not be any such global db variables! Again: it is explained in the documentation which you should read: https://doc.qt.io/qt-6/qsqldatabase.html
                                "Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior."

                                It is even in red color...

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

                                  Ah, Maybe I chose the wrong option for bind value from documentation.

                                  I can't change line with db, because it was make by my head-chef and it's important for anothers sql-questions which are in this app.

                                  1 Reply Last reply
                                  0
                                  • Karoluss96K Karoluss96

                                    @jsulm said in Get content of cell from QTableView:

                                    query.prepare(

                                    Now I have this:

                                    query = QSqlQuery(db)
                                     sql = query.prepare('INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,...) 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()) ]
                                                iden =query.BindValue(rowData[0], '')
                                                print (iden)
                                    ...
                                    

                                    with error:
                                    AttributeError: 'QSqlQuery' object has no attribute 'BindValue'

                                    db is a global value:

                                    db = QSqlDatabase.addDatabase("QOCISPATIAL")
                                    
                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by JonB
                                    #53

                                    @Karoluss96 said in Get content of cell from QTableView:

                                    iden =query.BindValue(rowData[0], '')

                                    AttributeError: 'QSqlQuery' object has no attribute 'BindValue'

                                    Please use your own efforts to resolve an error such as this, instead of just asking every time. The forum is not intended for people to type in responses to every error you might encounter. The method is named and shown as all examples as bindValue(), you now choose to type BindValue(), your own knowledge of Python should tell you what the issue is....

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

                                      @JonB said in Get content of cell from QTableView:

                                      Please use your own efforts to resolve an error such as this, instead of just asking every time. The forum is not intended for people to type in responses to every error you might encounter. The method is named and shown as all examples as bindValue(), you now choose to type BindValue(), your own knowledge of Python should tell you what the issue is....

                                      Eh... making so quick can't help in solving problem.

                                      Looking at the subject this discussion goes total out of topic.

                                      Have I continue it here or finish, not to spam for future's users?

                                      JonBJ 1 Reply Last reply
                                      0
                                      • Karoluss96K Karoluss96

                                        @JonB said in Get content of cell from QTableView:

                                        Please use your own efforts to resolve an error such as this, instead of just asking every time. The forum is not intended for people to type in responses to every error you might encounter. The method is named and shown as all examples as bindValue(), you now choose to type BindValue(), your own knowledge of Python should tell you what the issue is....

                                        Eh... making so quick can't help in solving problem.

                                        Looking at the subject this discussion goes total out of topic.

                                        Have I continue it here or finish, not to spam for future's users?

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

                                        @Karoluss96
                                        If it's still questions related to "Get content of cell from QTableView" you might as well continue in this topic.

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

                                          To be honest, I've tried some versions (look at 2 versions of sql variable ), but It haven't work yet.

                                          Now I have:

                                          sql = 'INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,ZAM...) VALUES ('
                                          #sql = query.prepare('INSERT INTO ZARZADZANIE.ZAMOWIENIA (ID,TERYT,ZAM...) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)')          
                                          

                                          and later:

                                          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 = rowData[0]
                                                          print (iden)
                                                      if (rowData[1] is None):    
                                                          teryt = query.bindValue(rowData[1],'')
                                                          print (teryt)
                                                      else:
                                                          teryt = rowData[1]
                                                          print (teryt)
                                                      if (rowData[2] is None):    
                                                          zam = query.bindValue(rowData[2],'')
                                                          print (zam)
                                          ...
                                          

                                          and take here error:

                                          dane2 =  iden + ',\'' + teryt + '\',\'' +zam +  ....+ '\''
                                                  print (dane2)
                                                  sqlCale = sql + dane2 + ')'
                                          

                                          can only concatenate str (not "NoneType") to str.

                                          On console for added data (in Table View, from which this question take values) I recive e.g. 7150,1401,UM14 if is fill or NONE it is not.

                                          So still the same, but bindValue separate None from filled attributes

                                          JonBJ 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