CWYAlpha

Just another WordPress.com site

Thought this was cool: python中使用数据库时的特殊字符问题

leave a comment »


这两天在弄个小程序,需要用数据库,程序没什么,都是轻车熟路,可是处理数据库时遇到了问题,要插入的数据有几百万行,数据一多,情况也就复杂了,有些数据里面包含有 ‘ , ” , ! 等特殊字符,导致insert select时出错。
刚开始遇到单引号出错,我就把sql语句里面换成双引号,后来遇到双引号我就没辙了,经过反复的测试,发现python的sql api已经能很好的解决这种问题了,关键代码如下:v = v.decode(‘utf-8’, ‘ignore’)
c.execute(“SELECT id FROM passwd WHERE value = ?”, [v])
 
p = p.decode(‘utf-8’, ‘ignore’)
c.execute(“INSERT INTO passwd(value) VALUES(?)”, [p])
from Lerry' Blog: http://lerry.me/post/2012/09/07/python-sql

Written by cwyalpha

十一月 24, 2012 在 3:44 下午

发表在 Uncategorized

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: