首页  >  问答  >  正文

python - 数据无法插入到mysql表里

# -*- conding:utf8 -*-
import tushare as ts
import pymysql
today_data = ts.get_today_all()
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='test')
cursor = conn.cursor() #创建游标
cursor.execute("INSERT INTO today_data(code_code,name_name,changepercent,trade,open_open,high_high,low_low,settlement,volume,turnoverratio,amount,per,pb_pb,mktcap,nmc) VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')",format(code_code,name_name,changepercent,trade,open_open,high_high,low_low,settlement,volume,turnoverratio,amount,per,pb_pb,mktcap,nmc))
cursor.close()

上面是代码,下面是报错信息

[Getting data:]#######################################Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/untitled/test.py", line 7, in <module>
    cursor.execute("INSERT INTO today_data(code_code,name_name,changepercent,trade,open_open,high_high,low_low,settlement,volume,turnoverratio,amount,per,pb_pb,mktcap,nmc) VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')",format(code_code,name_name,changepercent,trade,open_open,high_high,low_low,settlement,volume,turnoverratio,amount,per,pb_pb,mktcap,nmc))
NameError: name 'code_code' is not defined

test中是有today_data表的,表里也有这些字段,变量today_data获取到的数据也有这些字段,求指导

天蓬老师天蓬老师2741 天前434

全部回复(1)我来回复

  • 高洛峰

    高洛峰2017-04-18 10:28:48

    异常不是提示你了么?code_code 变量没有定义

    回复
    0
  • 取消回复