Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pymysql

This is the Python code where I am establishing a database connection. But I am getting a type error in my code. 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
like image 440
LAKSHMAN VINAY Avatar asked Dec 17 '22 11:12

LAKSHMAN VINAY


2 Answers

This solves the problem.

import pymysql

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             database='db',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
like image 104
Ramlakhan Kevat Avatar answered Jan 05 '23 17:01

Ramlakhan Kevat


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)
like image 31
FBA Gimhana Avatar answered Jan 05 '23 16:01

FBA Gimhana