[英]AttributeError: '_mysql.connection' object has no attribute 'cursor'
[英]python mysql: connection cursor has no execute attribute
在尝试一些mysql voodoo时,弹出以下错误。 我在这里阅读了其他问题,建议使用游标连接我已经做过的连接。 那么这里可能出什么问题了?
#!/usr/bin/env python
import os
import sys
import MySQLdb as db
try:
import json
from json import JSONDecodeError
except ImportError:
import simplejson as json
from simplejson import JSONDecodeError
connections = {
'conn1': db.connect('nimmsag_a', 'aix_regread', 'cmdbuser', 'aix_registry'),
'conn2': db.connect('localhost', 'ansible', 'ansible', 'ansible_inv')
}
connections['conn1'].cursor.execute('SELECT * FROM TABLES;')
connections['conn2'].cursor.execute('SELECT * FROM TABLES;')
[conn.close() for key, conn in connections.items()]
root@lpgaixmgmtlx01:/root>./reg2inv.py
Traceback (most recent call last):
File "./reg2inv.py", line 18, in <module>
connections['conn1'].cursor.execute('SELECT * FROM TABLES;')
AttributeError: 'function' object has no attribute 'execute'
作为记录 -和拼写在回溯所有的字母- connection.cursor
是返回一个函数Cursor
的对象。 您必须首先通过调用connection.cursor()
获得游标, 然后执行查询:
c1 = connections['conn1'].cursor()
c1.execute('SELECT * FROM TABLES')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.