Home  >  Q&A  >  body text

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 days ago436

reply all(1)I'll reply

  • 高洛峰

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

    Didn’t the exception alert you? code_code variable is not defined

    reply
    0
  • Cancelreply