. 1 Import PANDAS AS PD 2 Import numpy AS NP . 3 Import matplotlib MPL AS . 4 Import matplotlib.pyplot AS PLT . 5 mpl.rcParams [ ' font.sans serif- ' ] = [ ' Kaiti ' ] . 6 mpl.rcParams [ ' font.serif ' ] = [ ' Kaiti ' ] # common library into the need to use . 7 import pymysql # introduced mysql connection module . 8 Conn = pymysql.connect (= User ' the root ' , password =' Xxxxxx ' , Database = ' xxx ' ) # Set the connection username and password database 9 the Cursor = conn.cursor () # Set the cursor is set to normal inquiry 10 11 DEF MySQL (SQL): 12 cursor.execute (SQL) # execute a query 13 jieguo = cursor.fetchall () # see all results 14 cols = cursor.description # similar desc table_name returns results 15 COL = [] # create an empty list to store the column names 16 for v in cols: 17 COL .append (V [0]) #Recycling the extraction column name, and added to the empty list col 18 is dfsql = pd.DataFrame (jieguo, Columns = col) # converts the query result to a DF configuration, and reassigned to the column . 19 IF dfsql.empty: 20 is return ' empty SET ' # determine the query result is the value returned empty when 21 the else : 22 return dfsql # to return to DF structure query structure, DF.to_excel ... when you export query results with column names, this would resolve the mysql export the results directly without column names The problem
Query Example: