我有一个多行字符串(大型mysql查询),我想用字典(数据)格式化它,如下所示:
query = """
INSERT LOW_PRIORITY INTO goods(id, type, volume, strength,
country, price, description, year, image, url, class, color,
grape, vintage, dryness, shop, added)
VALUES (
'%(id)s',
'%(type)s',
'%(volume)s',
%(strength)d,
'%(country)s',
%(price)d,
'%(description)s',
'%(year)s',
'%(image)s',
'%(url)s',
'%(class)s',
'%(color)s',
'%(grape)s',
%(vintage)d,
'%(dryness)s',
'%(shop)s',
'%(added)s'
) ON DUPLICATE KEY UPDATE
type = '%(type)s',
volume = '%(volume)s',
strength = %(strength)d,
country = '%(country)s',
price = %(price)d,
description = '%(description)s',
year = '%(year)s',
image = '%(image)s',
url = '%(url)s',
class = '%(class)s',
color = '%(color)s',
grape = '%(grape)s',
vintage = %(vintage)d,
dryness = '%(dryness)s',
shop = '%(shop)s',
added = '%(added)s""" % data
当我试图这样做时,我没有得到任何错误,但我的字符串显示。有什么办法解决这个任务吗?在
更新: 我发现问题了。我试着用字典中的unicode值格式化ascii字符串。由于try/except块,我还没有看到回溯,因为try/except块调用了我的查询执行方法。故事的寓意是:总是检查python2.x中的字符串编码
你说“消失”是什么意思?如果发出
print query
,返回什么? 你好像错过了最后一行的一句话:它是不是应该是added = '%(added)s'""" % data
我怀疑你的数据字典少了一些键。在
例如:
相关问题 更多 >
编程相关推荐