reposter reposter - 1 year ago 116
Python Question

Python connect to MySQL error

Why do I keep getting this error

TypeError: __init__() takes 1 positional argument but 2 were given
when I want to connect Python with MySQL?

The exact error code is:


Traceback (most recent call last):
File "cone.py", line 4, in
conn = mysql.connector.connect(f)
File "C:\Python34\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 57, in __init__
super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given


Here's my code:

import mysql.connector

f="localhost","username","password","db"
conn = mysql.connector.connect(f)

c= conn.cursor()

c.execute("SELECT * FROM test")

rows=c.fetchall()

for eachRow in rows:
print (eachRow)

Answer Source

Try this:

conn = mysql.connector.connect(user="username",   password ="pwd", database="db")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download