Unicode charactered filename SELECT SQL参数问题

时间:2013-02-02 06:57:10

标签: python sql django unicode mysql-python

我在django中创建了一个webapp / webservice方法,它在数据库表上存储unicode字符文件名。我在views.py脚本上使用此方法执行此操作:

SubmissionContents(id=subid, filename=fn.decode('unicode_escape')).save()

我这样做是因为我遇到了一些unicode-charactered文件名,这些文件名没有正确存储在DB上(不管是什么原因,我还是不知道)。这也是关于stackoverflow thread/link的建议:

现在在后端,我有一个普通的python脚本,它使用MySQLdb-python模块来查询所述数据库。我的查询是这样的:

filename = (u"%s").encode('unicode_escape')
cursor.execute('select * from `submissioncontents` 
                where `submissioncontents`.`filename` = "%s"' 
                % filename.decode('unicode_escape'))

问题是没有返回匹配但是我非常确定DB-table是用这样的值填充的。我应该如何正确地进行查询?

提前致谢!

1 个答案:

答案 0 :(得分:0)

Check right syntax

cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])

代码:

cursor.execute('select * from `submissioncontents` 
                where `submissioncontents`.`filename` = %s' 
                , [ filename.decode('unicode_escape') ] )

引用文档:

  

“不要在原始查询上使用字符串格式!”