I have a MySQL table on which I'm executing SELECT statements within Python.
Is there anything out of the Python MySQLdb API that will, via the cursor, output an array of dictionaries whose keys are the column names (and values are those in the returned rows)?
Approach: Connect to a database using the connect() method. Create a cursor object and use that cursor object created to execute queries in order to create a table and insert values into it. Use the description keyword of the cursor object to get the column names.
You can list a table's columns with the mysqlshow db_name tbl_name command. The DESCRIBE statement provides information similar to SHOW COLUMNS .
fetchall() Method. The method fetches all (or all remaining) rows of a query result set and returns a list of tuples. If no more rows are available, it returns an empty list.
a line on a computer screen that moves to show the point where work is being done: You can move the cursor either by using the mouse or by using the arrow keys on the keyboard. Compare. pointer. Computer hardware.
Please use dictionary cursor:
cursor = conn.cursor (MySQLdb.cursors.DictCursor)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With