creat a list from sql query output with the attribute name instead of the index using python
Question: I have this code.

    
    
    cursor.execute("select id, name from client")
    clientids= cursor.fetchall()
    clientidList = []
    for clientid in clientids:
        #I can do that
        clientidList.append(clientid [0])
        #but I can't do that.
        clientidList.append(clientid ['id'])
    

Whit the second try I get an error `TypeError: 'tuple' object is not callable
`  
any idea why this is not possible? is there is any other way to accomplish
that, because it's more comprehensive when I put the attribute name than the
index, exactly in a query that has more than 20 columns output. I tried
[this](http://stackoverflow.com/questions/10195139/how-to-retrieve-sql-result-
column-value-using-column-name-in-python) but it didn't work for me

Thanks!


Answer: Try this:

    
    
    import mysql.connector
    
    db_config = {
        'user': 'root',
        'password': 'root',
        'port' : '8889',
        'host': '127.0.0.1',
        'database': 'clients_db'
    }
    cnx = {} # Connection placeholder
    
    cnx = mysql.connector.connect(**db_config)
    
    cur = cnx.cursor()
    cur.execute('SELECT id FROM client')
    
    columns = cur.column_names
    
    clientids = []
    
    for (entry) in cur:
        count = 0
        buffer = {}
    
        for row in entry:
            buffer[columns[count]] = row
            count += 1
    
        clientids.append(buffer)
    
    
    cur.close()
    
    clientidList = []
    
    for client in clientids:
       clientidList.append(client['id'])
    
    pprint.pprint(clientids)
    pprint.pprint(clientidList)
    

**Update**

Updated the code to select row names too. Not foolproof I guess. Test it some
:)

