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. is there any other function that i can use rather than .next()? because it reads only the last query
QtWS25 Last Chance

is there any other function that i can use rather than .next()? because it reads only the last query

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 702 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    def Submit_btn(self):
    USERNAME = self.username.text()
    PASSWORD = self.password.text()

        self.query = QSqlQuery("SELECT username,password FROM users")
            
        while (self.query.next()):
            user_data = self.query.value(0).toString()
            pass_data = self.query.value(1).toString()
    
        if USERNAME == user_data and PASSWORD == pass_data and USERNAME == "admin" and PASSWORD == "password":
            print "Login as Adminitrator"
        elif USERNAME == user_data and PASSWORD == pass_data:
            print "users mode"
        else:
            choice = QMessageBox.question(self,'IMS',"Incorrect username or password!",QMessageBox.Ok)
            if choice == QMessageBox.Ok:
                self.ctr = self.ctr + 1
                if self.ctr >= 3:
                    choice = QMessageBox.question(self,'IMS',"To many attemps System will terminate")
                    if choice == QMessageBox.Ok:
                        time.sleep(1.5)
                        sys.exit()
    

    i cant access the first and the second data if i have 3 records

    jsulmJ 1 Reply Last reply
    0
    • ? A Former User

      def Submit_btn(self):
      USERNAME = self.username.text()
      PASSWORD = self.password.text()

          self.query = QSqlQuery("SELECT username,password FROM users")
              
          while (self.query.next()):
              user_data = self.query.value(0).toString()
              pass_data = self.query.value(1).toString()
      
          if USERNAME == user_data and PASSWORD == pass_data and USERNAME == "admin" and PASSWORD == "password":
              print "Login as Adminitrator"
          elif USERNAME == user_data and PASSWORD == pass_data:
              print "users mode"
          else:
              choice = QMessageBox.question(self,'IMS',"Incorrect username or password!",QMessageBox.Ok)
              if choice == QMessageBox.Ok:
                  self.ctr = self.ctr + 1
                  if self.ctr >= 3:
                      choice = QMessageBox.question(self,'IMS',"To many attemps System will terminate")
                      if choice == QMessageBox.Ok:
                          time.sleep(1.5)
                          sys.exit()
      

      i cant access the first and the second data if i have 3 records

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

      @Gelo The problem is not next() the problem is that your code is wrong.
      You read user_data and pass_data in the while loop. That means if the loop is finished they will contain the data from last line returned by next().
      Should be (the whole stuff must be in the while loop, Python uses indentations to define blocks, so be careful):

      def Submit_btn(self):
          USERNAME = self.username.text()
          PASSWORD = self.password.text()
      
          self.query = QSqlQuery("SELECT username,password FROM users")
              
          while (self.query.next()):
              user_data = self.query.value(0).toString()
              pass_data = self.query.value(1).toString()
      
              if USERNAME == user_data and PASSWORD == pass_data and USERNAME == "admin" and PASSWORD == "password":
                 print "Login as Adminitrator"
              elif USERNAME == user_data and PASSWORD == pass_data:
                 print "users mode"
              else:
                  choice = QMessageBox.question(self,'IMS',"Incorrect username or password!",QMessageBox.Ok)
                  if choice == QMessageBox.Ok:
                     self.ctr = self.ctr + 1
                     if self.ctr >= 3:
                        choice = QMessageBox.question(self,'IMS',"To many attemps System will terminate")
                        if choice == QMessageBox.Ok:
                            time.sleep(1.5)
                            sys.exit()
      

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

      ? 1 Reply Last reply
      3
      • jsulmJ jsulm

        @Gelo The problem is not next() the problem is that your code is wrong.
        You read user_data and pass_data in the while loop. That means if the loop is finished they will contain the data from last line returned by next().
        Should be (the whole stuff must be in the while loop, Python uses indentations to define blocks, so be careful):

        def Submit_btn(self):
            USERNAME = self.username.text()
            PASSWORD = self.password.text()
        
            self.query = QSqlQuery("SELECT username,password FROM users")
                
            while (self.query.next()):
                user_data = self.query.value(0).toString()
                pass_data = self.query.value(1).toString()
        
                if USERNAME == user_data and PASSWORD == pass_data and USERNAME == "admin" and PASSWORD == "password":
                   print "Login as Adminitrator"
                elif USERNAME == user_data and PASSWORD == pass_data:
                   print "users mode"
                else:
                    choice = QMessageBox.question(self,'IMS',"Incorrect username or password!",QMessageBox.Ok)
                    if choice == QMessageBox.Ok:
                       self.ctr = self.ctr + 1
                       if self.ctr >= 3:
                          choice = QMessageBox.question(self,'IMS',"To many attemps System will terminate")
                          if choice == QMessageBox.Ok:
                              time.sleep(1.5)
                              sys.exit()
        
        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @jsulm it will always execute my else statement

        jsulmJ 1 Reply Last reply
        0
        • ? A Former User

          @jsulm it will always execute my else statement

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

          @Gelo Then check the data you get for USERNAME, PASSWORD and from the db.

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

          1 Reply Last reply
          1

          • Login

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