Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to access mysql database from python

I tried to access data from mysql database through python then I tried as

import MySQLdb

db = MySQLdb.connect(host = "127.0.0.1",
                     port = 3306,
                      user="Bishnu",
                      password = "Pulchowk",
                      db = "telecom_db")
cursor =db.cursor()
cursor.execute("select * from profile")
numrows = int(cursor.rowcount)
for x in range(numrows):
    row = cursor.fetchone()
    #if (row):
    print row[0], row[1]

cursor.close()

db.cose()

then it shows error

Traceback (most recent call last):
  File "D:\Bishnu\BE\4th year\7th semester\Major Project I\Workspace\Bishnuji\default\first.py", line 43, in <module>
    db = "telecom_db")
  File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
TypeError: 'password' is an invalid keyword argument for this function

Then I also tried removing password as

import MySQLdb

db = MySQLdb.connect(host = "127.0.0.1",
                     port = 3306,
                      user="Bishnu",
                      #password = "Pulchowk",
                      db = "telecom_db")
cursor =db.cursor()
cursor.execute("select * from profile")
numrows = int(cursor.rowcount)
for x in range(numrows):
    row = cursor.fetchone()
    #if (row):
    print row[0], row[1]

cursor.close()

db.cose()

still it shows error

Traceback (most recent call last):
  File "D:\Bishnu\BE\4th year\7th semester\Major Project I\Workspace\Bishnuji\default\first.py", line 43, in <module>
    db = "telecom_db")
  File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1044, "Access denied for user ''@'localhost' to database 'telecom_db'")

What may be the solution ?

like image 505
Bishnu Bhattarai Avatar asked Dec 16 '22 11:12

Bishnu Bhattarai


2 Answers

from the documentation seems that the correct naming of the parameter is passwd

like image 112
gipi Avatar answered Dec 18 '22 11:12

gipi


Check whether you have set password on your database or not. Normally user name and password are not changed and Default user name = 'root' and password filled is null i.e password = ''. If you have set password and forgot it then you can retrieve it by following instruction stated at How do I retrieve my MySQL username and password?

like image 38
nKandel Avatar answered Dec 18 '22 10:12

nKandel