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. how to create a login form in python how i can check if username and password is present in the database? this is my code
QtWS25 Last Chance

how to create a login form in python how i can check if username and password is present in the database? this is my code

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 11.6k 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 21 Jan 2017, 04:46 last edited by p3c0
    #1
    import sys
    import os
    import mysql.connector
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    
    class Form(QDialog):
        def __init__(self, parent=None):
            super(Form,self).__init__(parent)
    
            self.username = QLineEdit(self)
            self.QUserLabel = QLabel("USERNAME")
    
            self.password = QLineEdit(self)
            self.QPasswordLabel = QLabel("PASSWORD")
    
            self.btn_Submit = QPushButton("LOGIN")
    
            layout = QFormLayout()
            layout.addRow(self.QUserLabel,self.username)
            layout.addRow(self.QPasswordLabel,self.password)
            layout.addRow(self.btn_Submit)
    
            self.setLayout(layout)
    
            self.connect(self.btn_Submit, SIGNAL("clicked()"),self.Submit_btn)
    
        def Submit_btn(self):
            USERNAME = self.username.text()
            PASSWORD = self.password.text()
            
            con = mysql.connector.connect(user="root",password="admingelo",host="localhost",database="user")
            manager = con.cursor()
    
            get_query = ("""SELECT username,password FROM users WHERE username = '%s' AND password = '%s' """%str(USERNAME,PASSWORD))
            manager.execute(get_query)
            print get_query
            con.commit()
    
            if self.username.text() == USERNAME and self.password.text() == PASSWORD:
                print "hello"
            else:
                print "nothing match"
            
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()
    

    get_query = ("""SELECT username,password FROM users WHERE username = '%s' AND password = '%s' """%str(USERNAME,PASSWORD))
    TypeError: str() takes at most 1 argument (2 given)

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 21 Jan 2017, 05:08 last edited by
      #2

      @Gelo According to python documentation str() takes single argument but you have passed 2.

      157

      ? 1 Reply Last reply 21 Jan 2017, 05:14
      0
      • P p3c0
        21 Jan 2017, 05:08

        @Gelo According to python documentation str() takes single argument but you have passed 2.

        ? Offline
        ? Offline
        A Former User
        wrote on 21 Jan 2017, 05:14 last edited by p3c0
        #3

        @p3c0 this is my latest code

        import sys
        import os
        import mysql.connector
        from PyQt4.QtGui import *
        from PyQt4.QtCore import *
        from PyQt4.QtSql import *
        
        class Form(QDialog):
            def __init__(self, parent=None):
                super(Form,self).__init__(parent)
        
                self.username = QLineEdit(self)
                self.QUserLabel = QLabel("USERNAME")
        
                self.password = QLineEdit(self)
                self.QPasswordLabel = QLabel("PASSWORD")
        
                self.btn_Submit = QPushButton("LOGIN")
        
                layout = QFormLayout()
                layout.addRow(self.QUserLabel,self.username)
                layout.addRow(self.QPasswordLabel,self.password)
                layout.addRow(self.btn_Submit)
        
                self.setLayout(layout)
        
                self.connect(self.btn_Submit, SIGNAL("clicked()"),self.Submit_btn)
        
            def Submit_btn(self):
                USERNAME = self.username.text()
                PASSWORD = self.password.text()
                
                con = mysql.connector.connect(user="root",password="admingelo",host="localhost",database="user")
                manager = con.cursor()
        
                get_username = ("SELECT username FROM users WHERE username = '%s'"%str(USERNAME))
                get_password = ("SELECT password FROM users WHERE password = '%s'"%str(PASSWORD))
                manager.execute(get_username,get_password)
                print get_username
                print get_password
                #con.commit()
        
                if self.username.text() == get_username and self.password.text() == get_password:
                    print "ah"
                else:
                    print "nothing"
                
        app = QApplication(sys.argv)
        form = Form()
        form.show()
        app.exec_()
        
        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 21 Jan 2017, 05:15 last edited by
          #4

          @Gelo Great so was that the exact cause ?

          157

          ? 2 Replies Last reply 21 Jan 2017, 05:17
          1
          • P p3c0
            21 Jan 2017, 05:15

            @Gelo Great so was that the exact cause ?

            ? Offline
            ? Offline
            A Former User
            wrote on 21 Jan 2017, 05:17 last edited by
            #5

            @p3c0 it just print this instead to check if what i enter do exist

            RESTART: C:/Users/angelo/Desktop/IMS Delta EarthMoving Incorporated/QtLogin.py
            SELECT username FROM users WHERE username = 'admin'
            SELECT password FROM users WHERE password = 'password'

            1 Reply Last reply
            0
            • P p3c0
              21 Jan 2017, 05:15

              @Gelo Great so was that the exact cause ?

              ? Offline
              ? Offline
              A Former User
              wrote on 21 Jan 2017, 05:20 last edited by
              #6

              @p3c0 i want my script to check if what i enter is on database then if not it will print nothing or query not exist

              1 Reply Last reply
              0
              • P Offline
                P Offline
                p3c0
                Moderators
                wrote on 21 Jan 2017, 05:25 last edited by
                #7

                @Gelo Sorry I'm not much aware of PyQt but here is an example:
                http://www.codeprogress.com/python/libraries/pyqt/showPyQTExample.php?index=422&key=QSqlDatabaseConnecttoMySql

                db 	= QSqlDatabase.addDatabase("QMYSQL")
                db.setHostName("10.30.0.30")
                db.setDatabaseName("testDB")
                db.setUserName("root")
                db.setPassword("password")
                
                query = QSqlQuery ("SELECT * FROM test") 
                while (query.next()):
                    data = query.value(0).toString()
                

                You can try doing in the similar terms.

                157

                ? 1 Reply Last reply 21 Jan 2017, 05:39
                0
                • P p3c0
                  21 Jan 2017, 05:25

                  @Gelo Sorry I'm not much aware of PyQt but here is an example:
                  http://www.codeprogress.com/python/libraries/pyqt/showPyQTExample.php?index=422&key=QSqlDatabaseConnecttoMySql

                  db 	= QSqlDatabase.addDatabase("QMYSQL")
                  db.setHostName("10.30.0.30")
                  db.setDatabaseName("testDB")
                  db.setUserName("root")
                  db.setPassword("password")
                  
                  query = QSqlQuery ("SELECT * FROM test") 
                  while (query.next()):
                      data = query.value(0).toString()
                  

                  You can try doing in the similar terms.

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on 21 Jan 2017, 05:39 last edited by
                  #8

                  @p3c0 Thank you for your help i already solve the problem :)

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    p3c0
                    Moderators
                    wrote on 21 Jan 2017, 05:44 last edited by
                    #9

                    @Gelo Congratulations :) You can post the updated rectified code so that it might be helpful for others in the future.

                    157

                    ? 1 Reply Last reply 21 Jan 2017, 05:49
                    0
                    • P p3c0
                      21 Jan 2017, 05:44

                      @Gelo Congratulations :) You can post the updated rectified code so that it might be helpful for others in the future.

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on 21 Jan 2017, 05:49 last edited by
                      #10

                      @p3c0 Yeah sure :) Thank you my friend!

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        p3c0
                        Moderators
                        wrote on 21 Jan 2017, 06:03 last edited by
                        #11

                        Updated working code by @Gelo :

                        import sys
                        import os
                        import mysql.connector
                        from PyQt4.QtGui import *
                        from PyQt4.QtCore import *
                        from PyQt4.QtSql import *
                        
                        class Form(QDialog):
                            def __init__(self, parent=None):
                                super(Form,self).__init__(parent)
                        
                                self.db = QSqlDatabase.addDatabase("QMYSQL")
                                self.db.setHostName("localhost")
                                self.db.setDatabaseName("user")
                                self.db.setUserName("root")
                                self.db.setPassword("admingelo")
                                self.db.open()
                        
                                self.username = QLineEdit(self)
                                self.QUserLabel = QLabel("USERNAME")
                        
                                self.password = QLineEdit(self)
                                self.QPasswordLabel = QLabel("PASSWORD")
                                self.password.setEchoMode(QLineEdit.Password)
                        
                                self.btn_Submit = QPushButton("LOGIN")
                        
                                layout = QFormLayout()
                                layout.addRow(self.QUserLabel,self.username)
                                layout.addRow(self.QPasswordLabel,self.password)
                                layout.addRow(self.btn_Submit)
                        
                                self.setLayout(layout)
                        
                                self.connect(self.btn_Submit, SIGNAL("clicked()"),self.Submit_btn)
                        
                            def Submit_btn(self):
                                USERNAME = self.username.text()
                                PASSWORD = self.password.text()
                        
                                self.query = QSqlQuery("SELECT username,password FROM users")
                                while (self.query.next()):
                                    username = self.query.value(0).toString()
                                    password = self.query.value(1).toString()
                        
                                if USERNAME == username and PASSWORD == password:
                                    print "Login!"
                                else:
                                    print "failed"
                        
                                
                        app = QApplication(sys.argv)
                        form = Form()
                        form.show()
                        app.exec_()
                        

                        157

                        1 Reply Last reply
                        1
                        • J Offline
                          J Offline
                          jayrawatrj
                          wrote on 8 Oct 2018, 19:06 last edited by
                          #12

                          self.connect(self.btn_Submit, SIGNAL("clicked()"),self.Submit_btn)

                          what is "SIGNAL" here,??

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 8 Oct 2018, 19:40 last edited by
                            #13

                            Hi and welcome to devnet,

                            Please take a look at the new syntax documentation.

                            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
                            0
                            • J Offline
                              J Offline
                              jayrawatrj
                              wrote on 9 Oct 2018, 06:06 last edited by jayrawatrj 10 Sept 2018, 06:57
                              #14

                              thanks or reply.. instead of SIGNAL now we are using pyqtSignal() but here SIGNAL("clicked()") instead of clicked() what should i pass cause clicked () is giving error as

                              ""self.btn_Submit.clicked.connect(pyqtSignal('clicked'),self.Submit_btn)

                              TypeError: C++ type 'clicked' is not supported as a pyqtSignal() type argument type ""

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 9 Oct 2018, 21:18 last edited by
                                #15

                                Did you read PyQt's New-style Signal and Slot Support ?

                                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
                                0

                                • Login

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