python写入数据库_Python pymysql写入数据库报错

我在用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()

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

你可能感兴趣的