我有点好奇解决我的问题的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是否有更好的解决方案?
答案 0 :(得分:1)