Files
staceybot-template/libs/lib_mysql.py
2024-06-05 19:05:10 +02:00

44 lines
1.1 KiB
Python

import os
import mysql.connector
class MariaDB():
username = None
password = None
site_url = None
def __init__(self, mysql_host, mysql_user, mysql_databse, mysql_password):
self.mysql_host = mysql_host
self.mysql_user = mysql_user
self.mysql_password = mysql_password
self.mysql_databse = mysql_databse
def connect(self):
mydb = mysql.connector.connect(
host=self.mysql_host,
user=self.mysql_user,
password=self.mysql_password,
database=self.mysql_databse
)
self.mydb = mydb
self.mycursor = mydb.cursor(buffered=True, dictionary=True)
def insert(self, sql, val):
self.mycursor.execute(sql, val)
self.mydb.commit()
print(f"MySQL-Connection: {self.mycursor.rowcount} record inserted")
def select(self, sql, val):
if len(val) == 0:
self.mycursor.execute(sql)
else:
self.mycursor.execute(sql, val)
myresult = self.mycursor.fetchall()
return myresult
def disconnect(self):
self.mydb.disconnect()