ERROR - __init__() takes 1 positional argument but 5 were given

在新服务器上运行python时,得到如下错误。

ERROR - __init__() takes 1 positional argument but 5 were given

原因是因为,pymysql 版本不正确导致。

可正常运行环境为,python3 、pymysql 0.9.3 版本

运行错误的版本,python3 、pymysql 1.0.2版本

错误原因链接mysql数据库报错。

是因为新版的pymysql 链接方式发生了改变。

新版链接方式如下:

import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             database='db',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

with connection:
    with connection.cursor() as cursor:
        # Create a new record
        sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
        cursor.execute(sql, ('[email protected]', 'very-secret'))

    # connection is not autocommit by default. So you must commit to save
    # your changes.
    connection.commit()

    with connection.cursor() as cursor:
        # Read a single record
        sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
        cursor.execute(sql, ('[email protected]',))
        result = cursor.fetchone()
        print(result)

上述代码为官方给出的事例代码。

解决办法。

1、更换pymysql 版本,为0.9.3,即可正常运行

2、升级我们的pymysql链接方式。

你可能感兴趣的:(Mysql,Python,pycharm,ide,python)