Self is not passed to the class method in Python

Ahmed Eissa

I am trying the following python code:

import sqlite3
class database:

    def __init__(self):
        self.conn = sqlite3.connect("warehousedb.db")
        self.cursor = self.conn.cursor()
        self.cursor.execute(
            "CREATE TABLE IF NOT EXISTS `admin` (admin_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT)")
        self.cursor.execute(
            "CREATE TABLE IF NOT EXISTS `product` (product_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, product_name TEXT, product_qty TEXT, product_price TEXT)")
        self.cursor.execute("SELECT * FROM `admin` WHERE `username` = 'admin' AND `password` = 'admin'")

        if self.cursor.fetchone() is None:
            self.cursor.execute("INSERT INTO `admin` (username, password) VALUES('admin', 'admin')")
            self.conn.commit()


    def __del__(self):
        self.cursor.close()
        self.conn.close()

    def Execute_SQL(self, sql):
        self.cursor.execute(sql)
        self.conn.commit()

    def Get_SQL(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    def Get_SQL_One_Rec(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchone()

Then when trying this code:

db = database()    
st = "SELECT * FROM `admin` WHERE `username` = '" + USERNAME.get() + "' AND `password` = '" + PASSWORD.get() + "'"
rec = db.Get_SQL_One_Rec(st)

I am getting the following error:

rec = db.Get_SQL_One_Rec(st) TypeError: Get_SQL_One_Rec() missing 1 required positional argument: 'sql'

I can see from the Python Documentation that the Self object is automatically passed, so why I am getting this error? the error

SimonF

The code you have provided works, see this link:

https://repl.it/@zlim00/self-is-not-passed-to-the-class-method-in-python

The only difference compared to your code is that this sqllite database is in memory instead of a file. So if the code doesn't work for you, the error lies in parts of the code that you have not submitted.

Code (in case the link is removed):

import sqlite3
class database:
    def __init__(self):
        self.conn = sqlite3.connect(":memory:")
        self.cursor = self.conn.cursor()
        self.cursor.execute(
            "CREATE TABLE IF NOT EXISTS `admin` (admin_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT)")
        self.cursor.execute(
            "CREATE TABLE IF NOT EXISTS `product` (product_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, product_name TEXT, product_qty TEXT, product_price TEXT)")
        self.cursor.execute("SELECT * FROM `admin` WHERE `username` = 'admin' AND `password` = 'admin'")

        if self.cursor.fetchone() is None:
            self.cursor.execute("INSERT INTO `admin` (username, password) VALUES('admin', 'admin')")
            self.conn.commit()


    def __del__(self):
        self.cursor.close()
        self.conn.close()

    def Execute_SQL(self, sql):
        self.cursor.execute(sql)
        self.conn.commit()

    def Get_SQL(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    def Get_SQL_One_Rec(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchone()

if __name__ == '__main__':
    db = database()    
    st = "SELECT * FROM `admin` WHERE `username` = '" + 'admin' + "' AND `password` = '" + 'admin' + "'"
    rec = db.Get_SQL_One_Rec(st)
    print(rec)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

No self parameter passed in overrided python class' method

Self unpickling in class method in python

Python class with static method and reference to self

assigned lambda as method: self not passed?

Python annotation error when a method of a class get as input the class it self

What is self in Python? If a second class is inheriting another class and its method, what do the 'self's indicate?

Create method with class self variables outside of a python class and then add it as a method to class

Self referencing variables passed during the instantiation of a class

python: is it legal to pass self to the nested function inside the class method?

Python self.attribute cannot be seen when calling class method

can I bypass passing self object to a python class method?

Python - default value in class method and <code>self</code>

Python class: passing in self.var as a method argument yields no assignment?

Python method where first argument is either class or self

Using a Python method from another class without self

use current class without self in python (static method inherit)

Why class method must have self parameter in python?

How to run self referenced method from dictionary in python class?

Variables not being passed to class method

self.variable.function() meaning in python or accessing a method through a class self variable

Class method decorator with self arguments?

Class method decorator with self arguments?

Class method: 'self' not being read

self is not defined in class method argument

In Python, do assignment operators access class or instance variables when passed as a default value in a class method definition?

Python Class: overwrite `self`

Python: 'self' is missing (not automatically being passed) in object

How can I return self and another variable in a python class method while method chaining?

Class Method Swapping Keeping Old Class 'Self'?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive