记一次数据库连接错误(python之坑)

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'>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员进化不脱发!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值