According to the docs, the mysql-connector doesn't supply a dictionary-type
cursor, but they do give an approach to supplementing the functionality:
cursor.execute("""
SELECT last_name, first_name, hire_date
FROM employees WHERE emp_no = %s
""", (123,))
row = dict(zip(cursor.column_names, cursor.fetchone())
print("{last_name}, {first_name}: {hire_date}".format(row))
Their example only shows how you handle a single result at a time, but here
is how you could transform the entire result set into a list of dicts:
c.execute("""SELECT Artist_Name, Artist_ID, Login_ID FROM
artists_core.employee limit 1,10""")
res = [dict(zip(c.column_names, row) for row in c.fetchall()]
print res[0]['Artist_Name']
On Fri, Aug 24, 2012 at 1:13 AM, Donal McMullan <[email protected]>wrote:
> Hmm - looks like I dropped some inverted commas
>
>
> On 24/08/12 20:10, Donal McMullan wrote:
>
> import MySQLdb as mdb
> con=mdb.connect('172.16.48.9', 'showtime', 's1000@ma25db', '
> artists_core')
> c = con.cursor(mdb.cursors.DictCursor)
> c.execute("""SELECT Artist_Name, Artist_ID, Login_ID FROM
> artists_core.employee limit 1,10""")
> rows = c.fetchall()
> for row in rows:
> print "%s %s" % (row["Artist_Name"], row["Artist_ID"])
>
>
>
> --
>
> Donal McMullan
> Production Engineer
>
> --
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings:
> http://groups.google.com/group/python_inside_maya/subscribe
>
--
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings:
http://groups.google.com/group/python_inside_maya/subscribe