Python pymysql写入数据库报错

 RaymondKit 发布于 2022-10-28 10:28

我在用Python pymysql写入数据库时一直报错:
pymysql.err.InternalError: (1366, u"Incorrect decimal value: 'xa0' for column 'zuojiesuan' at row 1")
百度的结果是编码问题,我不知道具体怎么解决?

-- coding: utf-8 --

import urllib2
import pymysql
import time
from bs4 import BeautifulSoup

today = time.strftime('%Y-%m-%d',time.localtime(time.time()))

loginHeaders = {

'Host':'www.czce.com.cn',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
'Referer':'http://www.czce.com.cn/portal/jysj/mrhq/A091101index_1.htm',
'Content-Type':'text/html',
'Connection':'keep-alive'

}
url='http://www.czce.com.cn/portal...'
request = urllib2.Request(url, headers=loginHeaders)
opener = urllib2.build_opener()
response = opener.open(request)
content = response.read().decode('gbk').replace("","").replace("","")
soup = BeautifulSoup(content,'lxml').findAll(id="senfe")[0]
table = soup.text.split("n")
conn = pymysql.connect(host="192.168.1.98",user="root",passwd="root",db="test",charset='utf8')
cur = conn.cursor()
'''
cur.execute('create table zhujiagewang(id INT(10),'

        'updatetime TIMESTAMP, '
        'collecttime DATE,'
        'pinzhongyuefen VARCHAR(10),'
        'zuojiesuan DECIMAL(10,2),'
        'jinkaipan DECIMAL(10,2),'
        'zuigaojia DECIMAL(10,2),'
        'zuidijai DECIMAL(10,2),'
        'jinshoupan DECIMAL(10,2),'
        'jinjiesuan DECIMAL(10,2),'
        'zhangdie1 DECIMAL(10,2),'
        'zhangdie2 DECIMAL(10,2),'
        'chengjiaoliang INT(10),'
        'kongpanliang INT(10),'
        'zengjianliang INT(10),'
        'chengjiaoe DECIMAL(20,2))')

'''
for i in range(0,8):

value=[today,table[16+14*i],table[17+14*i],table[18+14*i],table[19+14*i],table[20+14*i],
       table[21+14*i],table[22+14*i],table[23+14*i],table[24+14*i],table[25+14*i],
       table[26+14*i],table[27+14*i],table[28+14*i]]

cur.execute("INSERT INTO zhujiagewang"

        "(collecttime,pinzhongyuefen,zuojiesuan,jinkaipan,zuigaojia,zuidijai,"
        "jinshoupan,jinjiesuan,zhangdie1,zhangdie2,chengjiaoliang,kongpanliang,zengjianliang,chengjiaoe)"
        "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",value)

conn.commit()
cur.close()
conn.close()

1 个回答
  • 你的做结算这列是DECIMAL类型,你插入一个字符串'xa0',当然会报错,你要把'xa0'转成整数或者浮点数才行啊

    2022-10-29 14:27 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有