Karthik Vg
Karthik Vg

Reputation: 108

connecting database using pymysql in python

I'm still learning how to work with a databases using python libraries

An operational error has occurred while executing the following code screenshot

Code snippet:

import pymysql
db1=pymysql.connect(host="127.0.0.1",user="root",password='root',port=3306,db='world')

a=db1.cursor()

sql='SELECT * from CITY'
print(a.execute(sql))

Error:

(1045, "Access denied for user 'root'@'localhost' (using password: NO)")

what could be the reason for this?

Upvotes: 1

Views: 877

Answers (1)

Const
Const

Reputation: 6653

An operational error has occurred while executing the following code screenshot. what could be the reason for this?

  • Error in question is:

    (1045, "Access denied for user 'root'@'localhost' (using password: NO)")
    

    It reveals that you are attempting to connect without password while you actually supplied one in connection arguments. This was at one point known bug with pymysql so answer can be version dependent. If you can normally connect to mysql with mysql -u root -proot world (database, password and user from your connect arguments) that means that your database is indeed with proper credentials and expects password while connect method is not sending one, most probably due to old version. On the other hand if you can't connect then most probably your root password is blank and you need to set it properly. Also there is option that you have issues with access to database 'world' for root user.

Upvotes: 1

Related Questions