属性错误:模块';MySQL';没有属性';连接器';
我正在运行MacOS Mojave,试图通过Spyder ide(Python3)使用MySQL-python连接器连接到MySQL
但我收到错误:
File "/Users/admin/Desktop/omgboi.py", line 5, in <module>
mydb=mysql.connector.connect(host='localhost',user='root',passwd='hariom21feb',database='test')
AttributeError: module 'mysql' has no attribute 'connector'
我尝试通过bash安装和重新安装MySQL-Connector和MySQL-Connector-python,但不起作用。我还尝试更改了文件的名称,但效果不佳。代码如下:
import mysql.connector
import random
first_names=['Jodee,Marielle,Phillip,Colby,Stephany,Dione,Grover,Napoleon,Nicholas,Alysa,Noma,Leta,Ciera,Donny,Buc,Iren,Renato,Glory,Stacia,Bennie,Soo,Mitzie,Kaci,Peggy,Hilma,Melva,Cindie,Miyoko,Melina,Cammy,Blanche,Rhea,Jill,Kellye,Ailene,Vida,Alva,Sau,Hollis,Oswaldo,Marty']
last_names=['Bula,Bibi,Rolf,Tayna,Ardith,Art,Jeannetta,Patrina,Ronny,Maida,Cleopatra,Sherry,Vincenza,Sheri,Sherlyn,Shayne,Geneva,Javier,Celine,Saran,Shari,Boris,Gwyneth,Summer,Maryellen,Rufina,Essie,Palma,Rafael,Cordell,Jude,Jenine,Manuel,Cleveland,Daphine,Lavina,Candi,Rossie,Brunilda,Gilberte,Nick,Hoyt,Lucius,Ardis,Tyler,Dwain,Caleb,Aide,Mckinley,Margurite']
mydb=mysql.connector.connect(host='localhost',user='root',passwd='hariom21feb',database='test')
mycursor=mydb.cursor()
fkey=random.randint(0,49)
lkey=random.randint(0,49)
nkey=random.randint(0,50)
for i in range(nkey):
query=('insert into test value(%s,%s,%s,%s,%s)')
tup=('L5W4NW',first_names(fkey),last_names(lkey),'2970','completed')
mycursor.execute(query,tup)
mydb.commit()
知道我做错了什么吗?
解决方案
您是如何安装的?
PyPI包mysql-connector
已弃用,应使用官方mysql-connector-python
。
最简单也是最推荐的方法是使用pip
:
pip install mysql-connector-python
有关安装的详细信息,请点击此处https://dev.mysql.com/doc/dev/connector-python/8.0/installation.html
相关文章