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

Viewed 14398

This is the Python code where I am establishing a database connection. But I am getting a type error in my code. Please tell me what is the issue.

import pymysql

connnection = pymysql.connect("localhost", "root", "", "dbname")

Error:

Traceback(most recent call last):

 File "c:/Users/Dell/Desktop/Full Code/COllectUrls.py", line 21, in <module>

   connnection = pymysql.connect("localhost", "root", "", "dbname")

TypeError: _ init _() takes 1 positional argument but 5 were given
4 Answers

Try this

import pymysql

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             database='db',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

This Will work for 3.x versions

import pymysql

connection = pymysql.connect(host='localhost',user='root',password='',database='db_emp',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)


cursor = connection.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print ("Database version : %s " % data)
                             
                            
                            
import pymysql

conn=pymysql.connect(host='localhost',user='root',password='12345678',database='studentdb')

Try this:

import pymysql

con = pymysql.connect(host='localhost',
        user='user7',
        password='s$cret',
        db='testdb',
        charset='utf8mb4')
Related