python mysql 中文出错_python含中文字符串插入mysql失败

在执行这段语句是出现了下面的错误,弄了一晚上没解决,哪位大神帮忙看下

con=MySQLdb.connect(host="127.0.0.1",port=3306,user="root",passwd="root",db="mysql")

cur=con.cursor()

for line in new_insert:

rank,forum,title,author,follow,title_time=line

print chardet.detect(rank)

print chardet.detect(forum)

print chardet.detect(title)

print chardet.detect(author)

print chardet.detect(follow)

print chardet.detect(title_time)

forum=forum.decode('ascii').encode('utf-8')

author=author.decode('ascii').encode('utf-8')

follow=follow.decode('ascii').encode('utf-8')

title_time=title_time.decode('ascii').encode('utf-8')

cur.execute("SET NAMES utf8")

sql="INSERT INTO bbs VALUES(%s,%s,%s,%s,%s,%s)"%(rank,forum,title,author,follow,title_time)

result=cur.execute(sql)

if result!=1:

print rank,forum,title,author,forum,"insert error"

con.rollback()

continue

con.commit()

cur.close()

con.close()

{'confidence': 0.7525, 'encoding': 'utf-8'}

{'confidence': 1.0, 'encoding': 'ascii'}

{'confidence': 0.99, 'encoding': 'utf-8'}

{'confidence': 1.0, 'encoding': 'ascii'}

{'confidence': 1.0, 'encoding': 'ascii'}

{'confidence': 1.0, 'encoding': 'ascii'}

Traceback (most recent call last):

File "E:/spider/main.py", line 6, in

cre.run()

File "E:\spider\bbs_top10.py", line 91, in run

result=cur.execute(sql)

File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute

self.errorhandler(self, exc, value)

File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler

raise errorclass, errorvalue

_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 \xe5\x90\x8d,Girls,\xe8\xaf\xb7\xe5\xa4\xa7\xe7\xa5\x9e\xe5\xb8\xae\xe5\xbf\x99\xe7\x9c\x8b\xe7\x9c\x8b\xe5\x92\x8c\xe5\xa5\xb3\xe5\xad\xa9\xe5\xad\x90\xe7\x9a\x84\xe8\x81\x8a\xe5\xa4\xa9\xe9\x97\xae\xe9\xa2\x98\xe5\x87\xba\xe5\x9c\xa8\xe5\x93\xaa\xe9\x87\x8c\xe5\x91\xa2,niha' at line 1")

Process finished with exit code 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值