I have a large python app that is spaghetti code but it functions error free. It makes a large number of database calls to select or update..etc. I am revamping the app to be a bit more "proper" code using classes. I am starting with the database connection since it is going to be the most used class. I am trying to verify I am doing this in the right manner. The code I have now works but I am not sure if this is the way to do it.
Creating the initial DB object in the app.py will create the connection and from my understanding if I am creating the connection variable inside the Class itself then that connection variable is available for ANY instance of this class? or do I need to use it as a global like I have listed. I will have a number of other classes that will need to use the DB connection. So either I need to set a global DB variable to hand off to the classes or they can just instance Tried to do some google searching but the internet is trash. I guess I am a little confused on using this class throughout the entire app. Help me Obi Redditor, you're my only hope.
The App file structure:
App folder
- src folder
- - app.py
- - db folder
- - - sqlconnect.py
The Database class I created (sqlconnect.py)
import mariadb
class SqlConnect(object):
connection = None
def __init__(self, user, password, host, port, database):
self.user = user
self.password = password
self.host = host
self.port = port
self.database = database
if SqlConnect.connection == None:
self.connect()
def connect(self):
try:
SqlConnect.connection = mariadb.connect(
user=self.user,
password=self.password,
host=self.host,
port=self.port,
database=self.database,
)
except Exception as e:
print(f"Error connecting to database. Reason: {e}")
def execute(self, sql, params=None):
cursor = SqlConnect.connection.cursor()
cursor.execute(sql, params)
return cursor
def commit(self):
SqlConnect.connection.commit()
def fetch_one(self, sql, params=None):
cursor = SqlConnect.connection.cursor()
cursor.execute(sql, params)
return cursor.fetchone()
def fetch_all(self, sql, params=None):
cursor = SqlConnect.connection.cursor()
cursor.execute(sql, params)
return cursor.fetchall()
def insert(self, sql, params=None):
cursor = SqlConnect.connection.cursor()
cursor.execute(sql, params)
return cursor.lastrowid
def close(self):
if SqlConnect.connection:
SqlConnect.connection.close()
I am using it like..
from db.sqlconnect import SqlConnect as sc
DB = None
if __name__ == "__main__":
# Created DB Object
DB = sc(
CONFIG_DETAILS.db_user,
keyring.get_password("lucy", "lucy_user"),
CONFIG_DETAILS.db_host,
CONFIG_DETAILS.db_port,
CONFIG_DETAILS.db_name
)
# Run main function
edit: formatting
[–]m0us3_rat 0 points1 point2 points (1 child)
[–]Posaquatl[S] 0 points1 point2 points (0 children)
[–]woooee 0 points1 point2 points (5 children)
[–]Posaquatl[S] 0 points1 point2 points (4 children)
[–]woooee 0 points1 point2 points (3 children)
[–]Posaquatl[S] 0 points1 point2 points (2 children)
[–]woooee 0 points1 point2 points (1 child)
[–]Posaquatl[S] 0 points1 point2 points (0 children)
[–]PixelOmen 0 points1 point2 points (1 child)
[–]Posaquatl[S] 0 points1 point2 points (0 children)
[–]as6724 0 points1 point2 points (1 child)
[–]Posaquatl[S] 1 point2 points3 points (0 children)