Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. What's wrong with my code?
QtWS25 Last Chance

What's wrong with my code?

Scheduled Pinned Locked Moved Unsolved Language Bindings
python
19 Posts 3 Posters 6.1k 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:
                    if USERNAME == "admin" and PASSWORD == "password":
                        print "Login as Administrator"
                        return True
                    if USERNAME != "admin" and PASSWORD != "password":
                        print "Log is as User"
                    
            if USERNAME != user_data and PASSWORD != pass_data:
                print "wrong"
                self.ctr += 1
                print self.ctr
                if self.ctr >= 10:
                    sys.exit()
    

    If i enter wrong input with users account the increament for wrong input wont count

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      I don't know python

      def Submit_btn(self):
              USERNAME = self.username.text()
              PASSWORD = self.password.text()
              
              self.query = QSqlQuery()
              self.query.prepare("SELECT username FROM users WHERE username= ? and password = ?")
              self.query.addBindValue(USERNAME)    
              self.query.addBindValue(PASSWORD) 
              if(!self.query.exec()):
                  print "Database query failed"
                  return False
              if(self.query.next()):
                      if USERNAME == "admin":
                          print "Login as Administrator"
                      else:
                          print "Log is as User"
                      return True
      
                  print "wrong"
                  self.ctr += 1
                  print self.ctr
                  if self.ctr >= 10:
                      sys.exit()
                  return False
      

      P.S.
      And this kids is how you do user authentication completely wrong!

      You should store the salt (a random string) for each account in plain text or encrypted in the database, retrieve it, (decrypt it if it's encrypted) and use it with the supplied password in a QCryptographicHash or with somthing better now compare this hash with the one in the database

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      ? 2 Replies Last reply
      5
      • VRoninV VRonin

        I don't know python

        def Submit_btn(self):
                USERNAME = self.username.text()
                PASSWORD = self.password.text()
                
                self.query = QSqlQuery()
                self.query.prepare("SELECT username FROM users WHERE username= ? and password = ?")
                self.query.addBindValue(USERNAME)    
                self.query.addBindValue(PASSWORD) 
                if(!self.query.exec()):
                    print "Database query failed"
                    return False
                if(self.query.next()):
                        if USERNAME == "admin":
                            print "Login as Administrator"
                        else:
                            print "Log is as User"
                        return True
        
                    print "wrong"
                    self.ctr += 1
                    print self.ctr
                    if self.ctr >= 10:
                        sys.exit()
                    return False
        

        P.S.
        And this kids is how you do user authentication completely wrong!

        You should store the salt (a random string) for each account in plain text or encrypted in the database, retrieve it, (decrypt it if it's encrypted) and use it with the supplied password in a QCryptographicHash or with somthing better now compare this hash with the one in the database

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @VRonin sorry newbie to programming anyways thank you! i will try that later :)

        1 Reply Last reply
        0
        • VRoninV VRonin

          I don't know python

          def Submit_btn(self):
                  USERNAME = self.username.text()
                  PASSWORD = self.password.text()
                  
                  self.query = QSqlQuery()
                  self.query.prepare("SELECT username FROM users WHERE username= ? and password = ?")
                  self.query.addBindValue(USERNAME)    
                  self.query.addBindValue(PASSWORD) 
                  if(!self.query.exec()):
                      print "Database query failed"
                      return False
                  if(self.query.next()):
                          if USERNAME == "admin":
                              print "Login as Administrator"
                          else:
                              print "Log is as User"
                          return True
          
                      print "wrong"
                      self.ctr += 1
                      print self.ctr
                      if self.ctr >= 10:
                          sys.exit()
                      return False
          

          P.S.
          And this kids is how you do user authentication completely wrong!

          You should store the salt (a random string) for each account in plain text or encrypted in the database, retrieve it, (decrypt it if it's encrypted) and use it with the supplied password in a QCryptographicHash or with somthing better now compare this hash with the one in the database

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by VRonin
          #4

          @VRonin

          if(!self.query.exec()):
                      print "Database query failed"
                      return False
          

          ! not working syntax error

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            I used a C++ boolean operation instead of a python one, as mentioned I can't use python. easy to look up to be fair...

            if(not self.query.exec()):
                        print "Database query failed"
                        return False
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            ? 1 Reply Last reply
            1
            • VRoninV VRonin

              I used a C++ boolean operation instead of a python one, as mentioned I can't use python. easy to look up to be fair...

              if(not self.query.exec()):
                          print "Database query failed"
                          return False
              
              ? Offline
              ? Offline
              A Former User
              wrote on last edited by VRonin
              #6

              @VRonin

              if(not self.query.exec()):
                          print "Database query failed"
                          return False
              

              print statement not showing up i also try the "not" function sorry i forgot to indicate

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                It should not show up, if it did it meant I messed something up in the query

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                ? 1 Reply Last reply
                1
                • VRoninV VRonin

                  It should not show up, if it did it meant I messed something up in the query

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by A Former User
                  #8

                  @VRonin i want to block inputs if username or password is not existing in my database i try to use isActive but behaves the same then if i remove the "not" function it executes when i type the correct query

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    its if(self.query.next()): that checks if the username and password exists or not

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    ? 1 Reply Last reply
                    1
                    • VRoninV VRonin

                      its if(self.query.next()): that checks if the username and password exists or not

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      @VRonin i try to remove the if(self.query.exec_()) and modify with .next() but still not showing my print statement account doesn't exist

                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        no, you don't have to modify anything.

                         if(!self.query.exec()):
                                    print "Database query failed"
                                    return False
                        

                        checks that the database ran the query correctly, it says nothing on the data inside it. the next block

                         if(self.query.next()):
                                        if USERNAME == "admin":
                                            print "Login as Administrator"
                                        else:
                                            print "Log is as User"
                                        return True
                        

                        here we check if the username and passwords were in the database

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        ? 1 Reply Last reply
                        3
                        • VRoninV VRonin

                          no, you don't have to modify anything.

                           if(!self.query.exec()):
                                      print "Database query failed"
                                      return False
                          

                          checks that the database ran the query correctly, it says nothing on the data inside it. the next block

                           if(self.query.next()):
                                          if USERNAME == "admin":
                                              print "Login as Administrator"
                                          else:
                                              print "Log is as User"
                                          return True
                          

                          here we check if the username and passwords were in the database

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          @VRonin i understand now Thank you very much! i remove the "not" function and it works

                          ? 1 Reply Last reply
                          0
                          • ? A Former User

                            @VRonin i understand now Thank you very much! i remove the "not" function and it works

                            ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #13

                            @Gelo but the problem is everytime i input a correct query for example the admin account it prints the "login as admin" and but after it print the admin mode "Database query failed" shows up first

                            1 Reply Last reply
                            0
                            • VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by
                              #14

                              could you post your current code?

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              ? 1 Reply Last reply
                              2
                              • VRoninV VRonin

                                could you post your current code?

                                ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #15

                                @VRonin

                                def Submit_btn(self):
                                    USERNAME = self.username.text()
                                    PASSWORD = self.password.text()
                                    
                                    self.query = QSqlQuery()
                                    self.query.prepare("SELECT username FROM users WHERE username = '%s' and password = '%s'"%(USERNAME,str(PASSWORD)))
                                    self.query.addBindValue(USERNAME)    
                                    self.query.addBindValue(PASSWORD)
                                    
                                    if(self.query.exec_()):
                                        print "Database query failed"
                                        self.ctr += 1
                                        print self.ctr
                                    if self.ctr >= 3:
                                        print "3 wrong attemps will terminate in a second!"
                                        time.sleep(2)
                                        sys.exit()
                                        return False
                                    if(self.query.next()):
                                            if USERNAME == "admin":
                                                print "Login as Administrator"
                                                print self.query.exec_()
                                                subprocess.Popen("__init__.py",shell=True)
                                                sys.exit()
                                            else:
                                                print "View Mode!"
                                                sys.exit()
                                            return True
                                
                                VRoninV the_T 2 Replies Last reply
                                0
                                • ? A Former User

                                  @VRonin

                                  def Submit_btn(self):
                                      USERNAME = self.username.text()
                                      PASSWORD = self.password.text()
                                      
                                      self.query = QSqlQuery()
                                      self.query.prepare("SELECT username FROM users WHERE username = '%s' and password = '%s'"%(USERNAME,str(PASSWORD)))
                                      self.query.addBindValue(USERNAME)    
                                      self.query.addBindValue(PASSWORD)
                                      
                                      if(self.query.exec_()):
                                          print "Database query failed"
                                          self.ctr += 1
                                          print self.ctr
                                      if self.ctr >= 3:
                                          print "3 wrong attemps will terminate in a second!"
                                          time.sleep(2)
                                          sys.exit()
                                          return False
                                      if(self.query.next()):
                                              if USERNAME == "admin":
                                                  print "Login as Administrator"
                                                  print self.query.exec_()
                                                  subprocess.Popen("__init__.py",shell=True)
                                                  sys.exit()
                                              else:
                                                  print "View Mode!"
                                                  sys.exit()
                                              return True
                                  
                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by VRonin
                                  #16

                                  @Gelo said in Whats wrong with my code!:

                                  self.query.prepare("SELECT username FROM users WHERE username = '%s' and password = '%s'"%(USERNAME,str(PASSWORD)))

                                  http://www.w3schools.com/sql/sql_injection.asp

                                  if(self.query.exec_()):

                                  why did you remove the not? it should be if(not self.query.exec_()):

                                  print "3 wrong attemps will terminate in a second!"

                                  you are not checking the attempts in the in the right place

                                  print self.query.exec_()

                                  why are you executing the query again?

                                  if(not self.query.exec_()):
                                  	print "Database query failed"
                                  else:
                                  	if(self.query.next()):
                                  		if USERNAME == "admin":
                                  			print "Login as Administrator"
                                  			print self.query.exec_()
                                  			subprocess.Popen("__init__.py",shell=True)
                                  			sys.exit()
                                  		else:
                                  			print "View Mode!"
                                  			sys.exit()
                                  		return True
                                  	else:
                                  		self.ctr += 1
                                  		print self.ctr
                                  		if self.ctr >= 3:
                                  			print "3 wrong attemps will terminate in a second!"
                                  			time.sleep(2)
                                  			sys.exit()
                                  		return False
                                  

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  ? 1 Reply Last reply
                                  0
                                  • VRoninV VRonin

                                    @Gelo said in Whats wrong with my code!:

                                    self.query.prepare("SELECT username FROM users WHERE username = '%s' and password = '%s'"%(USERNAME,str(PASSWORD)))

                                    http://www.w3schools.com/sql/sql_injection.asp

                                    if(self.query.exec_()):

                                    why did you remove the not? it should be if(not self.query.exec_()):

                                    print "3 wrong attemps will terminate in a second!"

                                    you are not checking the attempts in the in the right place

                                    print self.query.exec_()

                                    why are you executing the query again?

                                    if(not self.query.exec_()):
                                    	print "Database query failed"
                                    else:
                                    	if(self.query.next()):
                                    		if USERNAME == "admin":
                                    			print "Login as Administrator"
                                    			print self.query.exec_()
                                    			subprocess.Popen("__init__.py",shell=True)
                                    			sys.exit()
                                    		else:
                                    			print "View Mode!"
                                    			sys.exit()
                                    		return True
                                    	else:
                                    		self.ctr += 1
                                    		print self.ctr
                                    		if self.ctr >= 3:
                                    			print "3 wrong attemps will terminate in a second!"
                                    			time.sleep(2)
                                    			sys.exit()
                                    		return False
                                    
                                    ? Offline
                                    ? Offline
                                    A Former User
                                    wrote on last edited by
                                    #17

                                    @VRonin i remove the not function because it does not print the message if i enter wrong inputs

                                    1 Reply Last reply
                                    0
                                    • VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by VRonin
                                      #18

                                      if(not self.query.exec()): checks that the query did run correctly, it does not check your input.
                                      The input is checked by if(self.query.next()): if that is true then username and password were found in the database.

                                      Please do not overlook the SQL injection bug:

                                      self.query.prepare("SELECT username FROM users WHERE username = '%s' and password = '%s'"%(USERNAME,str(PASSWORD)))
                                      http://www.w3schools.com/sql/sql_injection.asp

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      1 Reply Last reply
                                      0
                                      • ? A Former User

                                        @VRonin

                                        def Submit_btn(self):
                                            USERNAME = self.username.text()
                                            PASSWORD = self.password.text()
                                            
                                            self.query = QSqlQuery()
                                            self.query.prepare("SELECT username FROM users WHERE username = '%s' and password = '%s'"%(USERNAME,str(PASSWORD)))
                                            self.query.addBindValue(USERNAME)    
                                            self.query.addBindValue(PASSWORD)
                                            
                                            if(self.query.exec_()):
                                                print "Database query failed"
                                                self.ctr += 1
                                                print self.ctr
                                            if self.ctr >= 3:
                                                print "3 wrong attemps will terminate in a second!"
                                                time.sleep(2)
                                                sys.exit()
                                                return False
                                            if(self.query.next()):
                                                    if USERNAME == "admin":
                                                        print "Login as Administrator"
                                                        print self.query.exec_()
                                                        subprocess.Popen("__init__.py",shell=True)
                                                        sys.exit()
                                                    else:
                                                        print "View Mode!"
                                                        sys.exit()
                                                    return True
                                        
                                        the_T Offline
                                        the_T Offline
                                        the_
                                        wrote on last edited by
                                        #19

                                        @Gelo said in Whats wrong with my code!:

                                            self.query = QSqlQuery()
                                            self.query.prepare("SELECT username FROM users WHERE username = '%s' and password = '%s'"%(USERNAME,str(PASSWORD)))
                                            self.query.addBindValue(USERNAME)    
                                            self.query.addBindValue(PASSWORD)
                                        

                                        I just wonder this works... did you read http://pyqt.sourceforge.net/Docs/PyQt4/qsqlquery.html for how to use prepare statement and binding values?

                                        To bind values to a prepared statement you need placeholders.
                                        According to the examples on http://pyqt.sourceforge.net/Docs/PyQt4/qsqlquery.html it should be done like this:

                                        Named binding using named placeholders:

                                         QSqlQuery query;
                                         query.prepare("INSERT INTO person (id, forename, surname) "
                                                       "VALUES (:id, :forename, :surname)");
                                         query.bindValue(":id", 1001);
                                         query.bindValue(":forename", "Bart");
                                         query.bindValue(":surname", "Simpson");
                                         query.exec();
                                        

                                        Positional binding using named placeholders:

                                         QSqlQuery query;
                                         query.prepare("INSERT INTO person (id, forename, surname) "
                                                       "VALUES (:id, :forename, :surname)");
                                         query.bindValue(0, 1001);
                                         query.bindValue(1, "Bart");
                                         query.bindValue(2, "Simpson");
                                         query.exec();
                                        

                                        Binding values using positional placeholders (version 1):

                                         QSqlQuery query;
                                         query.prepare("INSERT INTO person (id, forename, surname) "
                                                       "VALUES (?, ?, ?)");
                                         query.bindValue(0, 1001);
                                         query.bindValue(1, "Bart");
                                         query.bindValue(2, "Simpson");
                                         query.exec();
                                        

                                        Binding values using positional placeholders (version 2):

                                         QSqlQuery query;
                                         query.prepare("INSERT INTO person (id, forename, surname) "
                                                       "VALUES (?, ?, ?)");
                                         query.addBindValue(1001);
                                         query.addBindValue("Bart");
                                         query.addBindValue("Simpson");
                                         query.exec();
                                        

                                        -- No support in PM --

                                        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