尝试一些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'
答案 0 :(得分:2)
As documented-并用回溯中的所有字母拼写-connection.cursor
是一个返回Cursor
对象的函数。您必须先通过调用connection.cursor()
来获取光标,然后然后执行查询:
c1 = connections['conn1'].cursor()
c1.execute('SELECT * FROM TABLES')