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

查看:341
本文介绍了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获取到的数据也有这些字段,求指导

解决方案

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

这篇关于python - 数据无法插入到mysql表里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆