python - 获取到的数据生成新的mysql表
黄舟
黄舟 2017-04-18 10:26:53
0
3
595
# -*- 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的表中。把变量里的多个字段多条记录插入表里,或者直接新建一个表。

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
Ty80

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.

PHPzhong

The simplest thing is to use INSERT INTO... VALUES vars...

after processing the data
cursor.execute("INSERT INTO today_data (code、name、changepercent、trade、open high、low、settlement) VALUES ('{}','{}','{}','{}','{}','{}','{}')".format(code、name、changepercent、trade、open high、low、settlement))
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!