0

while trying out some mysql voodoo, the follwing error pops up. i read other questions here and the suggestions were to use a cursor for the connection which i already have done imho. so what could be wrong here?

#!/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'

1 Answer 1

2

As documented - and spelled in all letters in the traceback - connection.cursor is a function that returns a Cursor object. You must first get a cursor by calling connection.cursor(), then execute your queries:

c1 = connections['conn1'].cursor()
c1.execute('SELECT * FROM TABLES')
Sign up to request clarification or add additional context in comments.

2 Comments

connections['conn1'].cursor().execute('SELECT * FROM nodes;') connections['conn2'].cursor().execute('SELECT * FROM host;') seems to work
Usually you want to keep a reference on the cursor so you can get your query results ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.