Home > Blockchain >  What can I type for self parameter?
What can I type for self parameter?

Time:06-15

I created a database module for my app, but when iI gave username and password variables it returns

TypeError: Database._userlogin() missing 1 required positional argument: 'self'

MY FUNCTION WHICH IS IN MY CLASS

MY FUNCTION

CLASS METHOD

MY CLASS METHOD

THE WAY HOW I USE THIS (OTHER PY FILE)

THE WAY HOW I USE THIS

CodePudding user response:

Instead of calling

DataBaseClient._user_login(username=username, password=password)

You need to initialize your class and then call the method

client = DataBaseClient(...)
client._user_login(username=username, password=password)

Since this is a private method name, it should probably only be called by other methods in this class. In that case you use

def method_needing_login(self):
    self._user_login(self.username, self.password)

CodePudding user response:

HERE IS MY DATABASE CODE

import pymysql

class Database:
    def __init__(self):
        self.HOST = '127.0.0.1'
        self.USERNAME = 'root'
        self.PASSWORD = ''
        self.DATABASE = 'chatapp_test1'

    def _connect(self):
        try:
            self.connection = pymysql.connect(
            host= self.HOST,
            user= self.USERNAME,
            passwd= self.PASSWORD,
            database= self.DATABASE
        )

            self.cursor = self.connection.cursor()
            print('connected!')
            return self.connection

        
        except:
            raise
    

    def _createDatabase(self):
        try:
            self.cursor.execute("CREATE DATABASE IF NOT EXISTS chatapp_test1")
            self.cursor.execute("CREATE TABLE IF NOT EXISTS chatapp_admins (ID INT AUTO_INCREMENT NOT NULL,username varchar(255) NOT NULL, password varchar(255) NOT NULL, PRIMARY KEY (ID))")
            self.cursor.execute("CREATE TABLE IF NOT EXISTS chatapp_users (ID INT AUTO_INCREMENT NOT NULL, email varchar(255) NOT NULL, username varchar(255) NOT NULL, password varchar(255) NOT NULL, PRIMARY KEY (ID))")
            #self.cursor.execute("CREATE TABLE IF NOT EXISTS db_settings ()")
            print('Tables checked/created successfully')
        except:
            pass


    def _newuser(self, email, username, password):
        pass


    def _userlogin(self, username, password):
        
        while True:
            qry = """
            SELECT * FROM `smdb_admins` WHERE username ='{}' AND password ='{}';
            """
            qry = qry.format(username, password)
            self.cursor.execute(qry)
            self.connection.commit()
            data = self.cursor.fetchone()
            #print(data)

            if data:
                print("Giriş yapıldı!")
                break
            else:
                print("Kullanıcı adı veya şifre hatalı!")


#db = Database()
#db._connect()
#db._createDatabase()

THIS IS THE WHERE CODE IS RUNNING

    from database.database import Database as db


    class Ui_MainWindow(QMainWindow):
            def __init__(self):
                    super(Ui_MainWindow, self).__init__()
                    
                    self.setupUi()
            
            def login(self):
                    self.username = self.lineEdit.text()
                    self.password = self.lineEdit_2.text()
                    db._connect()
                    db._userlogin(username=self.username,password=self.password)
  • Related