首页 > 解决方案 > 尝试将 python 与 mysql 链接时出现错误

问题描述

TypeError: __init__() takes 1 positional argument but 5 were given

这是错误,我将在下面分享脚本:

import pymysql
print('---DATABASE CONNECTION SAMPLE---')
x=pymysql.connect('localhost','root','sha@123','avodha')
cr=x.cursor()

cr.execute('create table student(name char(50),age int)')

x.close()

标签: pythonmysqltypeerrorpymysql

解决方案


connect不采用位置参数,它采用关键字参数:

x = pymysql.connect(host='localhost',
                    user='user',
                    password='passwd',
                    database='avodha')

推荐阅读