属性错误:模块'MySQL'没有属性'连接器' [英] Attribute error:module 'mysql' has no attribute 'connector'

查看:112
本文介绍了属性错误:模块'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

这篇关于属性错误:模块&#39;MySQL&#39;没有属性&#39;连接器&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆