属性错误:模块“ mysql”没有属性“连接器”

时间:2019-05-30 15:35:35

标签: python mysql python-3.x spyder mysql-connector

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

关于我在做什么错的任何想法吗?

1 个答案:

答案 0 :(得分:0)

您是如何安装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