# -*- conding:utf8 -*- import tushare as ts import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='test') cursor = conn.cursor() #创建游标 today_data = ts.get_today_all() #把获取到的数据给today_data
代码如上,可以成功连接数据库,创建游标,获取到数据。
问题是:today_data获取到的数据有多个字段(code、name、changepercent、trade、open high、low、settlement),那如何在获取到数据的时候,生成一张新表?新表的字段和获取到的字段一致。
可能我描述的不够清楚,我是想把变量today_data里的数据插入到mysql的表中。把变量里的多个字段多条记录插入表里,或者直接新建一个表。
If you know all the fields, just create the table directly using SQL. You also know the table name, and then insert the data
It is easier to use the to_sql command of pandas, which can directly write the whole SQL table into a table.
This command has a parameter if_exists. If it is set to append, it will be created automatically if there is no table.
This is the simplest and most direct method for the data you obtain using tushare that is originally in pandas format.
The simplest thing is to use INSERT INTO... VALUES vars...
after processing the data