Frage

I have been getting this error when trying to match the username and password of my system on python using sql and qt

Traceback (most recent call last): File "F:/A2 Computing/Module 4 Project/COMP4 PROJECT/loginwindow.py", line 245, in get_value_from_user Correct_details= self.check_login_details(inputusername,inputpassword) File "F:/A2 Computing/Module 4 Project/COMP4 PROJECT/loginwindow.py", line 249, in check_login_details cur.execute("SELECT password FROM tblStaff WHERE username="+inputusername+"") TypeError: Can't convert 'Ui_Login' object to str implicitly

my code is below:

from PyQt4 import QtCore, QtGui
import sqlite3 as lite
import sys


con = lite.connect('Station Jewellers.db')

with con:

    cur = con.cursor()
    cur.execute("DROP TABLE IF EXISTS tblProductType") #Droppng deletes and remakes the table
    cur.execute("DROP TABLE IF EXISTS tblProducts")
    cur.execute("DROP TABLE IF EXISTS tblSale")
    cur.execute("DROP TABLE IF EXISTS tblRepairer")
    cur.execute("DROP TABLE IF EXISTS tblRepairInfo")
    cur.execute("DROP TABLE IF EXISTS tblRepairJob")
    cur.execute("DROP TABLE IF EXISTS tblItemSupplier")
    cur.execute("DROP TABLE IF EXISTS tblItemSupplied")
    cur.execute("DROP TABLE IF EXISTS tblItemType")
    cur.execute("DROP TABLE IF EXISTS tblItems")
    cur.execute("DROP TABLE IF EXISTS tblCustomer")
    cur.execute("DROP TABLE IF EXISTS tblRole")
    cur.execute("DROP TABLE IF EXISTS tblStaff")

    cur.execute("""CREATE TABLE tblStaff (
                firstname VARCHAR(50),
                surname VARCHAR (50),
                username VARCHAR(15),
                password VARCHAR(15),
                roleID SMALLINT,
                PRIMARY KEY (username),
                FOREIGN KEY (roleID) REFERENCES tblRoles(roleID)
                )""")

    cur.execute("""INSERT INTO tblStaff VALUES(
                'Tharran','Sukumar','Admin', 'Admin', 1)
                """)

    cur.execute("""INSERT INTO tblStaff VALUES(
                'John','Mayfield','Worker','Worker', 2)
                """)

    cur.execute("""CREATE TABLE tblRole (
                roleID SMALLINT,
                roleName VARCHAR(100),
                PRIMARY KEY (roleID))
                """)
    cur.execute("""INSERT INTO tblRole VALUES(
                '1', 'Shop Owner')
                """)
    cur.execute("""INSERT INTO tblRole VALUES(
                '2', 'Worker')
                """)

    cur.execute("""CREATE TABLE tblSale (
                saleID SMALLINT AUTO_INCREMENT,
                productID SMALLINT,
                firstname VARCHAR(15),
                PRIMARY KEY (saleID),
                FOREIGN KEY (productID) REFERENCES tblProducts(productID),
                FOREIGN KEY (firstname) REFERENCES tblStaff(firstname))
                """)

    cur.execute("""CREATE TABLE tblProducts (
                productID SMALLINT AUTO_INCREMENT,
                productName VARCHAR(100),
                price FLOAT,
                productTypeName VARCHAR(100),
                PRIMARY KEY (productID),
                FOREIGN KEY (productTypeName) REFERENCES tblProductType(productTypeName))
                """)

    cur.execute("""CREATE TABLE tblProductType (
                productTypeName VARCHAR(100),
                PRIMARY KEY (productTypeName))
                """)

    cur.execute("""CREATE TABLE tblCustomer (
                customerID SMALLINT AUTO_INCREMENT,
                title VARCHAR(10),
                forename VARCHAR(50),
                surname VARCHAR(50),
                address VARCHAR(100),
                city VARCHAR(50),
                postcode VARCHAR(10),
                emailAddress VARCHAR(200),
                phoneNumber VARCHAR(11),
                PRIMARY KEY (customerID))
                """)

    cur.execute("""CREATE TABLE tblRepairJob (
                repairID SMALLINT AUTO_INCREMENT,
                customerID SMALLINT,
                username VARCHAR(15),
                repair VARCHAR(3),
                PRIMARY KEY (repairID),
                FOREIGN KEY (username) REFERENCES tblStaff(username))
                """)

    cur.execute("""CREATE TABLE tblItemType (   
                itemTypeName VARCHAR(100),
                itemID SMALLINT,
                PRIMARY KEY (itemTypeName)
                FOREIGN KEY (itemID) REFERENCES tblItems(itemID))
                """)

    cur.execute("""CREATE TABLE tblRepairInfo   (
                repairNo SMALLINT AUTO_INCREMENT,
                repairInfo VARCHAR(2000),
                itemID SMALLINT,
                PRIMARY KEY (repairNo),
                FOREIGN KEY (itemID) REFERENCES tblItems(itemID))
                """)

    cur.execute("""CREATE TABLE tblItemSupplier (
                itemSupplierID SMALLINT AUTO_INCREMENT,
                title VARCHAR(10),
                forename VARCHAR(100),
                surname VARCHAR(100),
                address VARCHAR(100),
                city VARCHAR(50),
                postcode VARCHAR(10),
                emailAddress VARCHAR(200),
                phoneNumber VARCHAR(11),
                supplyDesc VARCHAR(100),
                PRIMARY KEY (itemSupplierID),
                FOREIGN KEY (supplyDesc) REFERENCES tblItemSupplied(supplyDesc))
                """)

    cur.execute("""CREATE TABLE tblItemSupplied (
                supplyDesc VARCHAR(100),
                stock SMALLINT,
                PRIMARY KEY (supplyDesc))
                """)

    cur.execute("""CREATE TABLE tblRepairer (
                repairerID SMALLINT AUTO_INCREMENT,
                title VARCHAR(10),
                forename VARCHAR(100),
                surname VARCHAR(100),
                address VARCHAR(100),
                city VARCHAR(50),
                postcode VARCHAR(10),
                emailAddress VARCHAR(200),
                phoneNumber VARCHAR(11),
                supplyDesc VARCHAR(100),
                PRIMARY KEY (repairerID))
                """)

    cur.execute("""CREATE TABLE tblItems (
                itemID SMALLINT AUTO_INCREMENT,
                itemDescription VARCHAR(100),
                repairerID SMALLINT,
                itemSupplierID SMALLINT,
                PRIMARY KEY (itemID),
                FOREIGN KEY (repairerID) REFERENCES tblRepairer(repairerID),
                FOREIGN KEY (itemSupplierID) REFERENCES tblItemSupplier(itemSupplierID))
                """)



try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Login(object):
    def setupUi(self, Login):
        Login.setObjectName(_fromUtf8("Login"))
        Login.resize(411, 223)
        Login.setStyleSheet(_fromUtf8("background-color: rgb(79, 129, 189);"))
        Login.setInputMethodHints(QtCore.Qt.ImhNone)
        Login.setSizeGripEnabled(False)
        self.layoutWidget = QtGui.QWidget(Login)
        self.layoutWidget.setGeometry(QtCore.QRect(70, 100, 271, 111))
        self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
        self.gridLayout = QtGui.QGridLayout(self.layoutWidget)
        self.gridLayout.setMargin(0)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.UsernameEdit = QtGui.QLineEdit(self.layoutWidget)
        self.UsernameEdit.setStyleSheet(_fromUtf8("background-color: rgb(255, 255, 255);"))
        self.UsernameEdit.setObjectName(_fromUtf8("UsernameEdit"))
        self.gridLayout.addWidget(self.UsernameEdit, 0, 0, 1, 1)
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.PasswordEdit = QtGui.QLineEdit(self.layoutWidget)
        self.PasswordEdit.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        self.PasswordEdit.setStyleSheet(_fromUtf8("background-color: rgb(255, 255, 255);"))
        self.PasswordEdit.setInputMethodHints(QtCore.Qt.ImhHiddenText|QtCore.Qt.ImhNoAutoUppercase|QtCore.Qt.ImhNoPredictiveText)
        self.PasswordEdit.setEchoMode(QtGui.QLineEdit.Password)
        self.PasswordEdit.setObjectName(_fromUtf8("PasswordEdit"))
        self.verticalLayout.addWidget(self.PasswordEdit)
        self.gridLayout.addLayout(self.verticalLayout, 1, 0, 1, 1)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.ExitButton = QtGui.QPushButton(self.layoutWidget)
        self.ExitButton.setStyleSheet(_fromUtf8("background-color: rgb(255, 255, 255);"))
        self.ExitButton.setObjectName(_fromUtf8("ExitButton"))
        self.horizontalLayout.addWidget(self.ExitButton)
        self.LoginButton = QtGui.QPushButton(self.layoutWidget)
        self.LoginButton.setStyleSheet(_fromUtf8("background-color: rgb(255, 255, 255);"))
        self.LoginButton.setObjectName(_fromUtf8("LoginButton"))
        self.horizontalLayout.addWidget(self.LoginButton)
        self.gridLayout.addLayout(self.horizontalLayout, 2, 0, 1, 1)
        self.LoginTitle = QtGui.QLabel(Login)
        self.LoginTitle.setGeometry(QtCore.QRect(130, 10, 154, 76))
        self.LoginTitle.setMinimumSize(QtCore.QSize(100, 10))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Palatino Linotype"))
        font.setPointSize(42)
        font.setBold(True)
        font.setWeight(75)
        self.LoginTitle.setFont(font)
        self.LoginTitle.setObjectName(_fromUtf8("LoginTitle"))
        self.label = QtGui.QLabel(Login)
        self.label.setGeometry(QtCore.QRect(10, 10, 81, 51))
        self.label.setStyleSheet(_fromUtf8("background-color: rgb(232, 232, 232);"))
        self.label.setObjectName(_fromUtf8("label"))

        self.retranslateUi(Login)
        QtCore.QObject.connect(self.ExitButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Login.close)
        QtCore.QObject.connect(self.LoginButton, QtCore.SIGNAL("clicked()"),self.get_value_from_user)
        QtCore.QMetaObject.connectSlotsByName(Login)
        Login.setTabOrder(self.LoginButton, self.UsernameEdit)
        Login.setTabOrder(self.UsernameEdit, self.PasswordEdit)
        Login.setTabOrder(self.PasswordEdit, self.ExitButton)



    def get_value_from_user(self):
        counter=0
        Correct_details = False
        while Correct_details==False and counter<3:
            inputusername=self.UsernameEdit.text()
            inputpassword=self.PasswordEdit.text()
            Correct_details= self.check_login_details(inputusername,inputpassword)
        QtGui.QMessageBox(self, 'Successful', "Succesfully Logged in as ", FirstName, Surname)

    def check_login_details(inputusername,inputpassword, self):
        cur.execute("SELECT password FROM tblStaff WHERE username="+inputusername+"")
        password=(cur.fetchone())
        password=password[2:-3]
        cur.execute("SELECT firstname FROM tblStaff WHERE username='"+inputusername+"'")
        FirstName=(cur.fetchone())
        FirstName=FirstName[2:-3]
        cur.execute("SELECT surname FROM tblStaff WHERE username='"+inputusername+"'")
        Surname=(cur.fetchone())
        Surname=Surname[2:-3]
        if (password) == (inputpassword):

            correct_details = True
            return correct_details
        else:
            QtGui.QMessageBox(self, 'Warning', "The Username or Password you have entered is incorrect")
            return correct_details



    def retranslateUi(self, Login):
        Login.setWindowTitle(_translate("Login", "Login", None))
        self.UsernameEdit.setPlaceholderText(_translate("Login", "Username", None))
        self.PasswordEdit.setPlaceholderText(_translate("Login", "Password", None))
        self.ExitButton.setText(_translate("Login", "Exit", None))
        self.LoginButton.setText(_translate("Login", "Login", None))
        self.LoginTitle.setText(_translate("Login", "Login", None))
        self.label.setText(_translate("Login", "<html><head/><body><p align=\"center\"><span style=\" font-size:10pt; font-weight:600;\">STATION</span></p><p align=\"center\"><span style=\" font-size:10pt; font-weight:600;\">JEWELLERS </span></p></body></html>", None))



if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Login = QtGui.QDialog()
    ui = Ui_Login()
    ui.setupUi(Login)
    Login.show()
    sys.exit(app.exec_())
War es hilfreich?

Lösung

The line def check_login_details(inputusername,inputpassword, self): should instead read def check_login_details(self, inputusername,inputpassword):

When dealing with an instance method definition (a method definednwithin a class), the self argument always comes first.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top