pymssql utf8:带斜杠的查询

时间:2016-03-03 10:45:01

标签: python sql-server python-2.7 unicode pymssql

我有点好奇解决我的问题的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是否有更好的解决方案?

1 个答案:

答案 0 :(得分:1)

我是个白痴!

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

r'bla\bla'
'bla\\bla'

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