Florian Lagg
Florian Lagg

Reputation: 751

pymssql utf8: queries with back slash

I'm a bit curious what is the pythonic/best way to solve my issue.

A short code example:

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

I want to know why the \ is missing in the first example - is there a better solution as quoting every single sql?

Upvotes: 1

Views: 1592

Answers (1)

Florian Lagg
Florian Lagg

Reputation: 751

I am an idiot!

Has nothing to do with mssql, it's just python strings.

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

Reference: https://docs.python.org/2.0/ref/strings.html

Upvotes: 1

Related Questions