Guest User

Untitled

a guest
Mar 17th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. def sql_query(query):
  2.  
  3.  
  4.     conn = pymysql.connect(host="hostname, user="user1", password="pass",
  5.                            database="dab1", port=3306)
  6.     cursor = conn.cursor()
  7.     cursor.execute(query)
  8.     rows = cursor.fetchall()
  9.     fields = []
  10.     for field in cursor.description:
  11.         fields.append(field[0])
  12.     fields = tuple(fields)
  13.     data = [i for i in rows]
  14.     df = pd.DataFrame.from_records(data, columns=fields)
  15.     cursor.close()
  16.     conn.close()
  17.     return df
Add Comment
Please, Sign In to add comment