initial commit
This commit is contained in:
40
database/mariadb.py
Normal file
40
database/mariadb.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import mariadb
|
||||
|
||||
import settings
|
||||
|
||||
|
||||
class MariadbConnector:
|
||||
def __init__(self):
|
||||
# Settings
|
||||
self.user = settings.MY_LOGIN
|
||||
self.password = settings.MY_PASSWORD
|
||||
self.host = settings.MY_HOST
|
||||
self.port = int(settings.MY_PORT)
|
||||
self.database = settings.MY_DATABASE
|
||||
|
||||
self.conn = self._get_connection()
|
||||
self.cursor = self._get_cursor()
|
||||
|
||||
def _get_connection(self) -> mariadb.connections.Connection:
|
||||
return mariadb.connect(user=self.user,
|
||||
password=self.password,
|
||||
host=self.host,
|
||||
port=self.port,
|
||||
database=self.database)
|
||||
|
||||
def _get_cursor(self) -> mariadb.cursors.Cursor:
|
||||
return self.conn.cursor()
|
||||
|
||||
def select(self, query_string: str, as_list=True):
|
||||
cursor = self.cursor
|
||||
|
||||
cursor.execute(query_string)
|
||||
if not as_list:
|
||||
return cursor.fetchone()
|
||||
return cursor.fetchall()
|
||||
|
||||
def insert(self, query_string) -> int:
|
||||
cursor = self.cursor
|
||||
|
||||
cursor.execute(query_string)
|
||||
return cursor.insert_id
|
||||
Reference in New Issue
Block a user