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. Whats the problem with this? if i enter wrong ID key error not showing up
Qt 6.11 is out! See what's new in the release blog

Whats the problem with this? if i enter wrong ID key error not showing up

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 3 Posters 15.9k Views 2 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

    @Aquarius171 said in Whats the problem with this? if i enter wrong ID key error not showing up:

    self.query.addBindValue(self.search.text())

    What error do you mean? If the ID is wrong the query will return an empty table - this is not an error.
    Why do you have

    self.query.addBindValue(self.search.text())
    

    if you already inserted the ID in the line above?

    A Offline
    A Offline
    Aquarius171
    wrote on last edited by
    #4

    @jsulm for example ID 79 is not present on my database if i enter 79 i want the error message prints or prompt an error message saying ID not found

    1 Reply Last reply
    0
    • A Aquarius171

      @jsulm the
      if(not self.query.exec_()):
      print "Database query failed"
      return False

      print statement is not showing up
      do i need to remove the addBindValue?

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

      @Aquarius171 Again: the query does not fail if it does not match anything. In SQL a SELECT statement returns 0..n lines. In your case, if the ID is not found you will get an empty result - there is no reason for the query to fail. If you want to know whether the ID was found use http://doc.qt.io/qt-5/qsqlquery.html#size
      Yes, you can remove self.query.addBindValue(self.search.text())

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • A Aquarius171

        def searchFunction(self):

            self.query = QSqlQuery()
            self.query.prepare("select * from incoming_mac_records where ID = '%s'"%str(self.search.text()))
            self.query.addBindValue(self.search.text())
        
            if(not self.query.exec_()):
                print "Database query failed"
                return False
            if(self.query.next()):
                self.projectModel.setQuery("select * from incoming_mac_records where ID = '%s'"%str(self.search.text()),self.db)
                self.projectView = QTableView(self)
                self.projectView.setModel(self.projectModel)
                self.projectView.resizeColumnsToContents()
                self.projectView.setGeometry(250,10,940,680)
                return True
        
        the_T Offline
        the_T Offline
        the_
        wrote on last edited by
        #6

        @Aquarius171 said in Whats the problem with this? if i enter wrong ID key error not showing up:

        def searchFunction(self):

            self.query = QSqlQuery()
            self.query.prepare("select * from incoming_mac_records where ID = '%s'"%str(self.search.text()))
            self.query.addBindValue(self.search.text())
        

        Thats complete nonsense and perfect for SQL injections
        If you have a look at the QSqlQuery documentation you could see that for prepare and addBindValue() you need paceholders.

        -- No support in PM --

        1 Reply Last reply
        1
        • jsulmJ jsulm

          @Aquarius171 Again: the query does not fail if it does not match anything. In SQL a SELECT statement returns 0..n lines. In your case, if the ID is not found you will get an empty result - there is no reason for the query to fail. If you want to know whether the ID was found use http://doc.qt.io/qt-5/qsqlquery.html#size
          Yes, you can remove self.query.addBindValue(self.search.text())

          A Offline
          A Offline
          Aquarius171
          wrote on last edited by
          #7

          @jsulm why the else is not working it is always True even if the ID doesn't exist

          self.query = QSqlQuery()
          self.query.prepare("select * from incoming_mac_records where ID = '%s'"%str(self.search.text()))

          if(self.query.isActive):
          print "Active"
          else:
          print "Not active"

          the_T 1 Reply Last reply
          0
          • A Aquarius171

            @jsulm why the else is not working it is always True even if the ID doesn't exist

            self.query = QSqlQuery()
            self.query.prepare("select * from incoming_mac_records where ID = '%s'"%str(self.search.text()))

            if(self.query.isActive):
            print "Active"
            else:
            print "Not active"

            the_T Offline
            the_T Offline
            the_
            wrote on last edited by
            #8

            @Aquarius171

            Where do you execute the query after preparing it?

            -- No support in PM --

            A 1 Reply Last reply
            0
            • the_T the_

              @Aquarius171

              Where do you execute the query after preparing it?

              A Offline
              A Offline
              Aquarius171
              wrote on last edited by
              #9

              @the_ What do you mean?

              the_T 1 Reply Last reply
              0
              • A Aquarius171

                @the_ What do you mean?

                the_T Offline
                the_T Offline
                the_
                wrote on last edited by
                #10

                @Aquarius171

                http://doc.qt.io/qt-5/qsqlquery.html#isActive

                As you can see in the docs, a query is only active while executing. So you always will get "Not active" in your code sample.

                And: did you have a look at the former link how to correctly use prepared statements?

                -- No support in PM --

                A 1 Reply Last reply
                0
                • the_T the_

                  @Aquarius171

                  http://doc.qt.io/qt-5/qsqlquery.html#isActive

                  As you can see in the docs, a query is only active while executing. So you always will get "Not active" in your code sample.

                  And: did you have a look at the former link how to correctly use prepared statements?

                  A Offline
                  A Offline
                  Aquarius171
                  wrote on last edited by
                  #11

                  @the_

                  search = self.search.text()

                      self.query = QSqlQuery()
                      self.query.prepare("select * from incoming_mac_records where ID = '%s'"%str(search))
                      self.query.addBindValue(search)
                  

                  if(not self.query.exec_()):
                  print "Not active"
                  if(self.query.next()):
                  print "Active"

                  how about this?

                  the_T 1 Reply Last reply
                  0
                  • A Aquarius171

                    @the_

                    search = self.search.text()

                        self.query = QSqlQuery()
                        self.query.prepare("select * from incoming_mac_records where ID = '%s'"%str(search))
                        self.query.addBindValue(search)
                    

                    if(not self.query.exec_()):
                    print "Not active"
                    if(self.query.next()):
                    print "Active"

                    how about this?

                    the_T Offline
                    the_T Offline
                    the_
                    wrote on last edited by
                    #12

                    @Aquarius171

                    you did not read/understand how to use prepare correctly?

                    
                    self.query = QSqlQuery()
                    self.query.prepare("select * from incoming_mac_records where ID=?")
                    self.query.addBindValue(self.search.text())
                    if not self.query.exec():
                      print self.query.lastError()
                    else:
                      while self.query.next():
                        #do whatever you need to do
                    

                    -- No support in PM --

                    A 3 Replies Last reply
                    1
                    • the_T the_

                      @Aquarius171

                      you did not read/understand how to use prepare correctly?

                      
                      self.query = QSqlQuery()
                      self.query.prepare("select * from incoming_mac_records where ID=?")
                      self.query.addBindValue(self.search.text())
                      if not self.query.exec():
                        print self.query.lastError()
                      else:
                        while self.query.next():
                          #do whatever you need to do
                      
                      A Offline
                      A Offline
                      Aquarius171
                      wrote on last edited by
                      #13

                      @the_ why this is not showing when i enter wrong input

                      if not self.query.exec(): #sysntax error so i make it like this self.query.exec_()
                      print self.query.lastError()

                      1 Reply Last reply
                      0
                      • the_T the_

                        @Aquarius171

                        you did not read/understand how to use prepare correctly?

                        
                        self.query = QSqlQuery()
                        self.query.prepare("select * from incoming_mac_records where ID=?")
                        self.query.addBindValue(self.search.text())
                        if not self.query.exec():
                          print self.query.lastError()
                        else:
                          while self.query.next():
                            #do whatever you need to do
                        
                        A Offline
                        A Offline
                        Aquarius171
                        wrote on last edited by
                        #14

                        @the_ and i try to use .isActive() and same happens but not returning error message like print "record not found"

                        1 Reply Last reply
                        0
                        • the_T the_

                          @Aquarius171

                          you did not read/understand how to use prepare correctly?

                          
                          self.query = QSqlQuery()
                          self.query.prepare("select * from incoming_mac_records where ID=?")
                          self.query.addBindValue(self.search.text())
                          if not self.query.exec():
                            print self.query.lastError()
                          else:
                            while self.query.next():
                              #do whatever you need to do
                          
                          A Offline
                          A Offline
                          Aquarius171
                          wrote on last edited by
                          #15

                          @the_
                          search = self.search.text()

                              self.query = QSqlQuery()
                              self.query.prepare("select * from incoming_mac_records where ID =?")
                              self.query.addBindValue(search)
                              
                              if not self.query.exec_():
                                  print self.query.lastError()
                              else:
                                  while self.query.next():
                          

                          if the ID key is active it show the print statement "active" but if not existing the self.query.lastError() not showing up

                          jsulmJ 1 Reply Last reply
                          0
                          • A Aquarius171

                            @the_
                            search = self.search.text()

                                self.query = QSqlQuery()
                                self.query.prepare("select * from incoming_mac_records where ID =?")
                                self.query.addBindValue(search)
                                
                                if not self.query.exec_():
                                    print self.query.lastError()
                                else:
                                    while self.query.next():
                            

                            if the ID key is active it show the print statement "active" but if not existing the self.query.lastError() not showing up

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

                            @Aquarius171 I already told you two times that QSqlQuery::exec() does NOT fail if the id is not found! Do you actually read what others write?
                            exec() only fails if your query is invalid (wrong SQL syntax) or there is no database connection. If the id is not found exec() will not fail (this is now the third and last time I tell this, sorry, but you really should read answers) - it will execute successfully and the result will be an empty table. A SQL SELECT statement NEVER fails if it does not find anything, instead it returns an empty result.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            A 1 Reply Last reply
                            4
                            • jsulmJ jsulm

                              @Aquarius171 I already told you two times that QSqlQuery::exec() does NOT fail if the id is not found! Do you actually read what others write?
                              exec() only fails if your query is invalid (wrong SQL syntax) or there is no database connection. If the id is not found exec() will not fail (this is now the third and last time I tell this, sorry, but you really should read answers) - it will execute successfully and the result will be an empty table. A SQL SELECT statement NEVER fails if it does not find anything, instead it returns an empty result.

                              A Offline
                              A Offline
                              Aquarius171
                              wrote on last edited by
                              #17

                              @jsulm for that thank you! and sorry :) GodBless sir ^_^ Have a nice day

                              jsulmJ 1 Reply Last reply
                              0
                              • A Aquarius171

                                @jsulm for that thank you! and sorry :) GodBless sir ^_^ Have a nice day

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

                                @Aquarius171 No problem! It's just that it is sometimes annoying if you repeat the same several times but the questioner does not read or ignore your explanation :-)
                                One tip: if you only need to know whether the ID is used use
                                SELECT count(*) FROM incoming_mac_records WHERE ID =?

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                A 2 Replies Last reply
                                1
                                • jsulmJ jsulm

                                  @Aquarius171 No problem! It's just that it is sometimes annoying if you repeat the same several times but the questioner does not read or ignore your explanation :-)
                                  One tip: if you only need to know whether the ID is used use
                                  SELECT count(*) FROM incoming_mac_records WHERE ID =?

                                  A Offline
                                  A Offline
                                  Aquarius171
                                  wrote on last edited by
                                  #19

                                  @jsulm how do i display count?

                                  1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @Aquarius171 No problem! It's just that it is sometimes annoying if you repeat the same several times but the questioner does not read or ignore your explanation :-)
                                    One tip: if you only need to know whether the ID is used use
                                    SELECT count(*) FROM incoming_mac_records WHERE ID =?

                                    A Offline
                                    A Offline
                                    Aquarius171
                                    wrote on last edited by
                                    #20

                                    @jsulm how to use isNull( int )?

                                    the_T jsulmJ 2 Replies Last reply
                                    0
                                    • A Aquarius171

                                      @jsulm how to use isNull( int )?

                                      the_T Offline
                                      the_T Offline
                                      the_
                                      wrote on last edited by the_
                                      #21

                                      @Aquarius171

                                      Again: you did not read and/or understand the PyQT QSqlQuery documentation??

                                      -- No support in PM --

                                      A 1 Reply Last reply
                                      1
                                      • A Aquarius171

                                        @jsulm how to use isNull( int )?

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

                                        @Aquarius171 If you're asking questions then please explain better!
                                        Where do you want to display count?
                                        Where do you want to use isNull() and for what data?

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        A 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @Aquarius171 If you're asking questions then please explain better!
                                          Where do you want to display count?
                                          Where do you want to use isNull() and for what data?

                                          A Offline
                                          A Offline
                                          Aquarius171
                                          wrote on last edited by
                                          #23

                                          @jsulm thank you already figure it out Thank you and sorry again sir ! :)

                                          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