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)
13
  • 1
    shouldn't that be :SELECT * FROM test Commented Aug 17, 2017 at 12:48
  • 1
    Is there a stack trace? Telling you which line caused the problem? Commented Aug 17, 2017 at 12:49
  • What examples are you following to get the connector.connect(f) syntax? Commented Aug 17, 2017 at 12:51
  • 1
    @cricket_007 Well I just tried it with MySQLdb.connect but it said that ImportError: No module named 'MySQLdb' Commented Aug 17, 2017 at 12:53
  • 1
    Than install it,are you on Python3? Commented Aug 17, 2017 at 12:53

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

Comments

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

7 Comments

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
what it says? >>> conn = mysql.connector.connect("localhost","dev","pwd","my_db") 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 (5 given) >>>
if you gave like this it will through an error.. otherwise it should work..
You're missing that the other error correctly says 5 positional arguments were given
@cricket_007 you OP missing or me?
|
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)

Comments

-1

Try this:

conn = mysql.connector.connect(host="host", user="username",   password ="pwd", database="db")

3 Comments

yeah tried it but then it said TypeError: __init__() takes 1 positional argument but 5 were given but thx for the answer
@Flo if you didnt provide arguments it will say like this.. so give assignment like user etc
@Flo Did you give keyword arguments? You have to. Read this: dev.mysql.com/doc/connector-python/en/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.