python - 无法从python访问mysql数据库

标签 python mysql mysql-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'")

可能的解决方案是什么?

最佳答案

来自 documentation似乎参数的正确命名是 passwd

关于python - 无法从python访问mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16127979/

相关文章:

python - 为什么 lambda 表达式结果对象的字典转换有问题?

python - spark中的高效对称计算

mysql - JOIN 结构的问题

mysql - 选择行 (“YYYYMMDD” 字符串格式)<= 今天

php - 有没有更简单的方法来编写此内容,以便显示最新条目而不是以前的条目?

MySQL 错误 0 : Retrieval of the RSA public key is not enabled for insecure connections

python - "Warning: Out of range value for column"将日期时间值插入 MySQL

Python循环效率

javascript - firepad:作为 firepad 的 codemirror div 中的默认文本的变量

python - mysql-python mac OSX 10.10 禁用安全身份验证