Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python3的Mysql连接器_Python 3.x - Fatal编程技术网

Python 3.x Python3的Mysql连接器

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

正在尝试为python3创建数据库类,但进展不太顺利。我拥有的当前代码给出了以下错误:

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
,但这是针对另一个线程的