【问题标题】:Python mysql.connector errorPython mysql.connector 错误
【发布时间】:2017-02-17 14:10:25
【问题描述】:

我在执行 python 脚本时遇到错误。

直接使用用户名和密码进行检查。 检查保存的用户并传递变量。

脚本:

import mysql.connector,sys
try:
    cnx = mysql.connector.connect (host='localhost', user = 'user', password = 'user@543', port= 3306)
except mysql.connector.errors.ProgrammingError, e:
    print time.strftime("%Y-%m-%d %H:%M:%S"),"Error %s" % (e)
    sys.exit(1)
except mysql.connector.errors.InterfaceError, e1:
    print time.strftime("%Y-%m-%d %H:%M:%S"),"Error %s" % (e1)
    sys.exit(1)
    cursor = db.cursor()
    cursor.execute("select version()")
    result_set = cursor.fetchall()
    print result_set

错误:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    cnx = mysql.connector.connect (host='localhost', user = 'user', password = 'user@543', port= 3306)
AttributeError: 'str' object has no attribute 'connector'

【问题讨论】:

  • 您是否在您发布的代码片段上方的任何位置使用mysql 作为变量名?
  • 不,我没有使用变量

标签: python mysql


【解决方案1】:

这是一个错字。 connect 和参数列表之间有一个空格。将行更改为:

cnx = mysql.connector.connect(
    host='localhost', user='user', password='user@543', port=3306
)

【讨论】:

    猜你喜欢
    • 2016-02-03
    • 2020-06-13
    • 2014-05-11
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多