0

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)
khelwood
  • 55,782
  • 14
  • 81
  • 108
reposter
  • 33
  • 1
  • 1
  • 7

4 Answers4

1

in python3.4 try the same using pymysql,

to install pip3 install PyMySQL

import pymysql
conn = pymysql.connect("localhost","dev","pwd","db_name")
cur = conn.cursor()

should connect like this in mysql.connector.connect

conn = mysql.connector.connect(host="localhost",user="uname", password="mypwd", database="emp_db")

For example,

>>> conn = mysql.connector.connect(f)Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/mysql/connector/__init__.py", line 98, in connect
    return MySQLConnection(*args, **kwargs)
TypeError: __init__() takes exactly 1 argument (2 given)
>>> conn = mysql.connector.connect(host="localhost",user="uname", password="pwd", database="reposter$tutorial_database")
>>> 

here is the Doc

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
  • thx for the answer but i already tried it with conn = mysql.connector.connect(host="localhost",user="uname", password="mypwd", database="emp_db") than it said:TypeError: __init__() takes 1 positional argument but 5 were given – reposter Aug 17 '17 at 13:00
  • what it says? >>> conn = mysql.connector.connect("localhost","dev","pwd","my_db") Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/dist-packages/mysql/connector/__init__.py", line 98, in connect return MySQLConnection(*args, **kwargs) TypeError: __init__() takes exactly 1 argument (5 given) >>> – Mohideen bin Mohammed Aug 17 '17 at 13:01
  • if you gave like this it will through an error.. otherwise it should work.. – Mohideen bin Mohammed Aug 17 '17 at 13:01
  • You're missing that the other error correctly says 5 *positional* arguments were given – OneCricketeer Aug 17 '17 at 13:02
  • @cricket_007 you OP missing or me? – Mohideen bin Mohammed Aug 17 '17 at 13:03
  • Traceback (most recent call last): File "cone.py", line 3, in conn = mysql.connector.connect("localhost","usernamer","pw","database") 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 5 were given – reposter Aug 17 '17 at 13:05
  • @Mohi try reproducing in Python34 – OneCricketeer Aug 17 '17 at 13:06
1

If you have managed to fix MySQLdb then you can connect like this

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)
MishaVacic
  • 1,812
  • 8
  • 25
  • 29
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)
-1

Try this:

conn = mysql.connector.connect(host="host", user="username",   password ="pwd", database="db")
reposter
  • 33
  • 1
  • 1
  • 7
Debanik Dawn
  • 797
  • 5
  • 28