Python 3.x Python3的Mysql连接器
正在尝试为python3创建数据库类,但进展不太顺利。我拥有的当前代码给出了以下错误:Python 3.x Python3的Mysql连接器,python-3.x,Python 3.x,正在尝试为python3创建数据库类,但进展不太顺利。我拥有的当前代码给出了以下错误: return self.cursor [Previous line repeated 995 more times] RecursionError: maximum recursion depth exceeded 我使用了thread作为参考,但我对使用Mysql/MariaDB更感兴趣。但我认为包装器本身与SQLite应该没有太大区别 这是我的密码 import mysql.connector from
return self.cursor
[Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded
我使用了thread作为参考,但我对使用Mysql/MariaDB更感兴趣。但我认为包装器本身与SQLite应该没有太大区别
这是我的密码
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()
我还注释掉了charset=utf8mb4
,因为出现了错误LookupError:unknown encoding:utf8mb4
,但这是针对另一个线程的