【问题标题】:Exception 'module has no attribute connector' - Python mysql异常'模块没有属性连接器' - Python mysql
【发布时间】:2014-07-26 13:26:24
【问题描述】:

我只是尝试批量添加一些数据。

我首先连接然后遍历字典项,创建每个查询以更新基于 mysql 的 zip_ids 表。以下是我的做法:

connection = _mysql.connect(host="xxx",user = "user", passwd="123", db="mine")
for id, zip in id_zip.items():
    query += """UPDATE zip_ids SET zip = %s id = %s;"""% (zip,id) 
print query    
try:
    cur = connection.cursor()
    connection.execute(query, multi=True)
    connection.commit()
    cur.close()
    connection.close()
    connection.disconnect()
except _mysql.connector.Error as err:
    print 'issue in Execution of adding zip', str(e)

问题是我不断抛出异常:

Exception 'module' object has no attribute 'connector'

如何解决此异常?它的原因是什么?批量添加数据的代码是否正确?

【问题讨论】:

  • 这里的_msql 是什么?那是从哪里进口的?大概except _mysql.connector.Error as err: 行抛出了异常,所以你需要告诉我们更多关于该模块来自哪里。
  • 忘记import mysql.connector

标签: python mysql


【解决方案1】:

它告诉你_mysql 模块没有名为connector 的属性。

我通常使用 ipython 来学习我一无所知的模块。

In [1]: import _mysql
In [2]: _mysql.conn

然后我按 Tab 键查看自动完成的内容并得到

_mysql.connect     _mysql.connection

_mysql.connection. 上的制表符完成显示 _mysql.connection.error 存在。然后我做

In [4]: ?_mysql.connection.error
Type:       method_descriptor
String Form:<method 'error' of '_mysql.connection' objects>
Docstring:
Returns the error message for the most recently invoked API function
that can succeed or fail. An empty string () is returned if no error
occurred.

至于添加批量数据,我认为您将两个不同 python 模块的 API 混合在一起。你正在使用_mysql,但我认为你真正想要的是MySQLdbhttp://mysql-python.sourceforge.net/MySQLdb.html

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2019-10-16
    • 2021-07-04
    • 2018-06-30
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2019-10-10
    相关资源
    最近更新 更多