Can't get MySQL Connector/Python to Return Dictionary -
i have python application, in i'm calling mysql stored procedure view, so:
import mysql.connector proc = 'audit_report' parms = [data['schoolid'], datetoiso(data['startdatedefault'],'from'), datetoiso(data['enddatedefault'],'to'), joinintlist(data['studypgms'], joinwith), joinintlist(data['fedpgms'], joinwith), joinintlist(data['statuses'], joinwith), data['fullssndefault']] conn = mysql.connector.connect(user='usr', database='db', password='pwd') cursor = conn.cursor(dictionary=true) cursor.callproc(proc, parms) result in cursor.stored_results(): print(result.fetchall())
i getting data returned list of tuples, standard output. since i'm using connector version 2.1.7, docs adding
dictionary=true
to cursor declaration should cause rowset returned list of dictionaries, column name key of each dictionary. main difference between application , example in docs i'm using cursor.callproc(), whereas examples use cursor.execute() actual sql code.
i tried
print(cursor.column_names)
to see if column names way, is
('@_audit_report_arg1', '@_audit_report_arg2', '@_audit_report_arg3', '@_audit_report_arg4', '@_audit_report_arg5', '@_audit_report_arg6', '@_audit_report_arg7')
which looks more input parameters stored procedure.
is there way column names of returned data? procedure complex , contains crosstab-type manipulation, calling same stored procedure mysql workbench happily supplies column names.
normally, knowing output supposed be, hard-code column names, except procedure crosstabs data last few columns, , unpredictable until after query runs. thanks...
you can use pymysql in python3 , should work fine !!
import pymysql.cursors connection = pymysql.connect(host='', user='', password='', db='test', charset='utf8mb4', cursorclass=pymysql.cursors.dictcursor) try: connection.cursor() cursor: # read single record sql = "query" cursor.execute(sql) result = cursor.fetchone() num_fields = len(cursor.description) field_names = [i[0] in cursor.description] print (field_names) finally: connection.close()
wiki
Comments
Post a Comment