简体   繁体   English

在Python 3.x中使用PyMySQL连接数据库

[英]Connection of database using PyMySQL in Python 3.x

Getting connection error in PyMYSQL: 在PyMYSQL中获取连接错误:

Error 错误

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='pymysql')
AttributeError: 'module' object has no attribute 'connect'

code

import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='pymysql')

cur = conn.cursor()

cur.execute("SELECT Host,User FROM user")

print(cur.description)

print()

for row in cur:
   print(row)

cur.close()
conn.close()

Use capital 'C' in pymysql.Connect. 在pymysql.Connect中使用大写字母“ C”。

conn = pymysql.Connect(host='127.0.0.1', port=3306, user='root', passwd='', db='pymysql')

The above statement should work. 上面的声明应该起作用。 It worked for me! 它为我工作!

You've called some other module "pymysql". 您已经将其他模块称为“ pymysql”。 Look for a file named "pymysql.py" and rename it, and remove any associated .pyc file. 查找名为“ pymysql.py”的文件并重命名,然后删除所有关联的.pyc文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM