Trying to make Database class for python3, but not going so well. The current code I have gives me the following error:
return self.cursor
[Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded
I have used this thread as a reference, but I'm more interested in using Mysql/MariaDB. But I figure that the wrapper itself shouldn't be that much of a difference from SQLite.
This is my code
import mysql.connector
from mysql.connector import Error
class Database:
def __init__(self):
try:
connection = mysql.connector.connect(
host='localhost',
database='db',
user='root',
# charset='utf8mb4',
passwd='')
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL database... MySQL Server version on ",db_Info)
cursor = connection.cursor(prepared=True)
cursor.execute("select database();")
record = cursor.fetchone()
print ("Your connected to - ", record[0])
except Error as e :
print ("Error while connecting to MySQL", e)
def __enter__(self):
return self
def __exit__(self):
if(connection.is_connected()):
cursor.commit()
cursor.close()
connection.close()
else:
print('Something went wrong...')
@property
def cursor(self):
return self.cursor
def commit(self):
self.connection.commit()
def execute(self, sql, params=None):
self.cursor.execute(sql, params or ())
def fetchall(self):
return self.cursor.fetchall()
def fetchone(self):
return self.cursor.fetchone()
def query(self, sql, params=None):
self.cursor.execute(sql, params or ())
return self.fetchall()
I have also commented out charset=utf8mb4
because of the error LookupError: unknown encoding: utf8mb4
, but thats for another thread...