3

Here is a the code I have so far:

from ConfigParser import *
import MySQLdb

configuration = ConfigParser()
configuration.read('someconfigfile.conf')
    db = MySQLdb.connect(
    host = configuration.get('DATABASE', 'MYSQL_HOST'),
    user = configuration.get('DATABASE', 'MYSQL_USER'),
    passwd = configuration.get('DATABASE', 'MYSQL_PASS'),
    db = configuration.get('DATABASE', 'MYSQL_DB'),
    port = configuration.getint('DATABASE', 'MYSQL_PORT'),
    ssl = {
        'ca':   configuration.get('SSL_SETTINGS', 'SSL_CA'),
        'cert': configuration.get('SSL_SETTINGS', 'SSL_CERT'),
        'key':  configuration.get('SSL_SETTINGS', 'SSL_KEY')
            },
    )
cursor = db.cursor()
sql = "SELECT column_name FROM information_schema.columns WHERE table_name='met';"
cursor.execute(sql)
list = cursor.fetchall()

print list

this is what prints:

(('field',), ('field',), ('field',), ('field',), ('field',), ('field',), ('field',), ('field',), ('field',), ('field',), ('field',), ('field',), ('field',), ('field',), ('pk_wind_spd2',), ('field',))

And I am getting a tuple instead of a list. I would prefer to have a list of strings

1 Answer 1

2

Don't shadow built-ins (list), change to

alist = cursor.fetchall()

This generator expression will get you the column names in a tuple:

tuple(i[0] for i in alist)

Sign up to request clarification or add additional context in comments.

2 Comments

Think you ment list(i[0] for i in alist) anyways I am giving a check mark for leading me in the right direction
@Richard: sure you can make it a list if you like. In that case you can simply use a list-comprehension and the code is even shorter: [i[0] for i in alist].

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.