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



  • 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)


  • Moderators

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



  • @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_()
    

  • Moderators

    @Gelo Great so was that the exact cause ?



  • @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'



  • @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


  • Moderators

    @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.



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


  • Moderators

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



  • @p3c0 Yeah sure :) Thank you my friend!


  • Moderators

    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_()
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.