I have a table that has about 50 columns. To retrieve the variables from the table I am doing:
cursor.execute("SELECT * FROM title WHERE vendor_id = '%s'"%vendor_id)
data=cursor.fetchone()
provider, language, type, subtype, vendor_id = data[0], data[1], data[2], data[3], data[4]
etc...
Is there a way to do this more concisely: the variables I want to define are also the names of the columns. Perhaps something like (in pseudocode) —
values=cursor.fetchone; columns=cursor.fetchcolumns()
data = zip(columns, values)
select *is going to be particularly challenging here as you don’t know what columns are being returned in which order. Depending on the database you’re using and the wrapper you’re using, you should be able to retrieve your rows as dicts instead which allows you to reference the columns as dict keys in the rows.For instance, MySQLdb supports this through a DictCursor. See http://www.kitebird.com/articles/pydbapi.html
For other libraries, they should offer a similar feature.