当我想将Python与MySQL连接时,为什么我一直收到此错误TypeError: __init__() takes 1 positional argument but 2 were given
?
确切的错误代码是:
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
这是我的代码:
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)
答案 0 :(得分:0)
在python3.4中尝试使用pymysql,
安装Intent
pip3 install PyMySQL
应该在import pymysql
conn = pymysql.connect("localhost","dev","pwd","db_name")
cur = conn.cursor()
mysql.connector.connect
例如,
conn = mysql.connector.connect(host="localhost",user="uname", password="mypwd", database="emp_db")
这是Doc
答案 1 :(得分:0)
如果您已设法修复MySQLdb,那么您可以像这样连接
import MySQLdb
mydb = MySQLdb.connect(host = 'localhost',user = 'flo',passwd = '********',db = 'yourdb')
cur = mydb.cursor()
command = cur.execute('SELECT * FROM yourtable')
results = cur.fetchall()
print (results)
答案 2 :(得分:0)
#import mysql connector
import mysql.connector
#declare your database variables
DBHOST = 'localhost'
DBNAME = 'database_name'
DBUSER = 'root'
DBPASS = ''
#establish the connection
connection = mysql.connector.connect(host=DBHOST, database=DBNAME, user=DBUSER, passwd=DBPASS)
答案 3 :(得分:-1)
试试这个:
conn = mysql.connector.connect(host="host", user="username", password ="pwd", database="db")