pymssql utf8:带反斜杠的查询

pymssql utf8: queries with back slash

我有点好奇 pythonic/best 解决我的问题的方法是什么。

一个简短的代码示例:

import pymssql
conn = pymssql.connect("SERVER", 'sa', 'PASSWORD', 'DATABASE', charset='utf8')

cursor = conn.cursor()

sql = "SELECT 'foo\bar' as bs_field"
cursor.execute(sql)
row = cursor.fetchone()
print row[0]
# missing \, returns u'foobar'

sql = "select FIELD_CONTAINING_BACKSLASH from TABLE"
cursor.execute(sql)
row = cursor.fetchone()
print row[0]
# all OK here

sql = "SELECT 'foo\bar' as bs_field"
cursor.execute(sql)
row = cursor.fetchone()
print row[0]
# this is OK too

我想知道为什么第一个示例中缺少 \ - 是否有更好的解决方案来引用每个 sql?

我是个白痴!

与mssql无关,只是python个字符串。

r'bla\bla'
'bla\bla'

参考:https://docs.python.org/2.0/ref/strings.html