异常“模块没有属性连接器"-Python mysql [英] Exception 'module has no attribute connector' - Python mysql
问题描述
我只是想批量添加一些数据.
I'm just attempting to add some data in bulk.
我首先连接然后遍历字典项,创建每个查询来更新 zip_ids 表,它是基于 mysql 的.以下是我的做法:
I firstly connect then iterate through dictionary items, creating each query to update the zip_ids table, which is mysql based. Below is how I am doing this:
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)
问题是我不断抛出异常:
Issue is that I keep getting an exception thrown:
异常 'module' 对象没有属性 'connector'
我该如何解决这个异常?它的原因是什么?批量添加数据的代码是否正确?
How can I resolve this exception? What is its cause? Is the code for adding data in bulk correct?
推荐答案
它告诉你 _mysql
模块没有名为 connector
的属性.
It's telling you that the _mysql
module has no attribute named connector
.
我通常使用 ipython 来学习我一无所知的模块.
I usually use ipython to learn about modules I know nothing about.
In [1]: import _mysql
In [2]: _mysql.conn
然后我按 Tab 键查看自动完成的内容,然后我得到
Then I hit the tab key to see what auto completes and I get
_mysql.connect _mysql.connection
对 _mysql.connection.
的 Tab 补全显示 _mysql.connection.error
存在.那我做
Tab-completing on _mysql.connection.
shows me that _mysql.connection.error
exists. Then I do
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
但我认为您真正想要的是 MySQLdb
:http://mysql-python.sourceforge.net/MySQLdb.html
As for adding bulk data, I think you're mixing up the APIs of two different python modules. You're using _mysql
but I think what you really want is MySQLdb
: http://mysql-python.sourceforge.net/MySQLdb.html
希望这会有所帮助!
这篇关于异常“模块没有属性连接器"-Python mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!