簡體   English   中英

無法從python訪問mysql數據庫

[英]unable to access mysql database from python

我嘗試通過python從mysql數據庫訪問數據,然后嘗試

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()

然后顯示錯誤

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

然后我也嘗試刪除密碼為

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()

仍然顯示錯誤

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'")

有什么解決方案?

文檔似乎參數的正確命名是passwd

檢查是否在數據庫上設置了密碼。 通常,用戶名和密碼不會更改,默認用戶名='root',密碼填充為null,即password =”。 如果您設置了密碼並忘記了密碼,則可以按照如何檢索MySQL用戶名和密碼中所述的說明進行檢索。

請卸載並使用卸載命令再次安裝mysqlclient

pip uninstall mysqlclient

安裝

pip install mysqlclient

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM