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