python连接mysql结果连不上,检测了半天,结果是多了个逗号改变了字符为元组的原因

1.报错

报错内容如下

Traceback (most recent call last):
  File "F:/aoi_gf_njl-main/测试脚本文件夹/cs.py", line 50, in <module>
    db.connect()
  File "F:/aoi_gf_njl-main/测试脚本文件夹/cs.py", line 26, in connect
    cursorclass=pymysql.cursors.DictCursor
  File "D:\Anaconda\envs\ocr\lib\site-packages\pymysql\connections.py", line 352, in __init__
    self.connect()
  File "D:\Anaconda\envs\ocr\lib\site-packages\pymysql\connections.py", line 617, in connect
    (self.host, self.port), self.connect_timeout, **kwargs
  File "D:\Anaconda\envs\ocr\lib\socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "D:\Anaconda\envs\ocr\lib\socket.py", line 752, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
TypeError: getaddrinfo() argument 1 must be string or None
self.user1 <class 'tuple'>

进程已结束,退出代码 1

1.报错的代码


import pymysql


class DatabaseHandler:
    def __init__(self, host, port, user, password, database):
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.database = database
        print("self.user1", type(self.user))

    def connect(self):
        """host, port, family, type, proto, flags"""

        try:

            self.connection = pymysql.connect(
                host=self.host,
                port=self.port,
                user=self.user,
                password=self.password,
                database=self.database,
                charset='utf8mb4',
                cursorclass=pymysql.cursors.DictCursor
            )

            print("连接数据库成功")
        except pymysql.Error as e:
            print(f"数据库连接错误: {e}")
            raise e

    def disconnect(self):
        if self.connection:
            self.connection.close()
            print("Disconnected from the database!")


if __name__ == '__main__':

    host = "自己的数据库地址",# 如 "11.136.241.77"
    user = "用户名",
    password = "密码",
    database = "数据库中要操作的库名"


    db = DatabaseHandler(host=host, port=3306, user=user, password=password,
                         database=database)
    db.connect()

2.正确的代码


import pymysql


class DatabaseHandler:
    def __init__(self, host, port, user, password, database):
        self.host = host
        self.port = port
        self.user = user
        self.password = password
        self.database = database
        print("self.user1", type(self.user))

    def connect(self):
        """host, port, family, type, proto, flags"""

        try:

            self.connection = pymysql.connect(
                host=self.host,
                port=self.port,
                user=self.user,
                password=self.password,
                database=self.database,
                charset='utf8mb4',
                cursorclass=pymysql.cursors.DictCursor
            )

            print("连接数据库成功")
        except pymysql.Error as e:
            print(f"数据库连接错误: {e}")
            raise e

    def disconnect(self):
        if self.connection:
            self.connection.close()
            print("Disconnected from the database!")


if __name__ == '__main__':

    host = "自己的数据库地址"# 如 "11.136.241.77"
    user = "用户名"
    password = "密码"
    database = "数据库中要操作的库名"


    db = DatabaseHandler(host=host, port=3306, user=user, password=password,
                         database=database)
    db.connect()

2.报错原因

多了个逗号成了元组,导致输入的用户名与密码等格式变了,大家看下面的例子就知道了

user = "guagua",
print("user", type(user))
#输出: user <class 'tuple'>

user = "guagua"
print("user", type(user))
#输出:user <class 'str'>

更多推荐