have been learning MySQL database recently, very interested. This time I made a simple attempt to use Python3.4 to interact with the MySQL database and upload a piece of stock data downloaded from a snowball online to the MySQL database. Only for beginners to provide reference, master Please do not laughed at.
The code has been uploaded to GitHub and you are welcome to follow:
https://github.com/JoshuaHe2015/Python_Code/blob/master/MySQL_test.py
1 ImportPymysql2f = Open (r'D:\Data\SZ000839.csv')#Load the CSV3Header =True4conn = Pymysql.connect ('localhost','Root','Password','Database')5Cur =conn.cursor ()6 7 forLineinchF:8 ifHeader:9Header = False#Skip the headerTen Else: Onedata = Line.replace ('\"',"'). Replace ('\ n',"'). Split (',') ACur.execute ("INSERT into SZ000839 (symbol,_date,_open,high,low,_close,volume) VALUES ('%s ', '%s ', '%s ', '%s ', '%s ', '%s ', '%s ') "%tuple (data)) - -Conn.commit ()#Commit The transaction theF.close ()#Don ' t forget to close all of these sources - cur.close () - conn.close () - Print('finished!')
Inserting data into the MySQL database with Python