gpt4 book ai didi

python - 属性错误:module 'mysql' has no attribute 'connector'

转载 作者:行者123 更新时间:2023-11-29 15:57:21 27 4
gpt4 key购买 nike

我正在运行 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()

关于我做错了什么有什么想法吗?

最佳答案

您是如何安装 Connector/Python 的?

PyPI 包 mysql-connector 已弃用,您应该使用官方 mysql-connector-python

最简单且推荐的方法是使用pip:

pip install mysql-connector-python

有关安装的更多详细信息,请参见此处 https://dev.mysql.com/doc/dev/connector-python/8.0/installation.html

关于python - 属性错误:module 'mysql' has no attribute 'connector' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56381315/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com