python - pymssql utf8 : queries with back slash

标签 python sql-server python-2.7 unicode pymssql

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

简短的代码示例:

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

关于python - pymssql utf8 : queries with back slash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35769945/

相关文章:

python - Matplotlib x 轴时间错误

Python:mac 中的绝对路径错误

sql-server - SSIS 数据传输添加字符(SQL Server 到 Postgres)

SQL按年龄获取客户

python-2.7 - python simpy错误返回生成器内部的值

python - 计算值标签 DICOM

python - 父标签的子标签有特定属性值时,如何使用BeautifulSoup获取父标签名称值?

SQL Server - 查找订单最多的前 n 个客户

python - "AssertionError: Only python 2.7 and later is supported by ptyprocess",在 python 2.6 上通过 pip 安装 pexpect 时

python - Python 中的 JSON 处理和在线 JSON Linters