Subhash Deshmukh
Subhash Deshmukh

Reputation: 360

Escaping characters from unicode string

I am trying to store u'\U0001f381' string in Azure sql server from python 2.7.11 in ubuntu 14.04 LTS. I have set the column type as nvarchar(MAX) so that it will accept unicode strings. following is the python script:

import pymssql
from creds import *
conn = pymssql.connect(host=HOST, user=USER, password=PASSWORD, database=DATABASE)
cursor = conn.cursor()

lst = [u'2017-07-04', u'\U0001f3e8', 1.0, 0.0, 0.0, 9.0]

print lst
placeholder = '%s,' * len(lst)

query = 'INSERT INTO Example_SearchAnalytics VALUES ( '+placeholder.rstrip(',')+ ')'

cursor.execute(query,tuple(lst))

conn.commit()

conn.close()

But I am getting following error when I execute above script from ubuntu environment.

pymssql.OperationalError: (105, "Unclosed quotation mark after the character string ''.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")

I don't get any error when I execute same script from windows environment. I think I need to escape any character from unicode string but I am not sure which. Please help.

Upvotes: 2

Views: 911

Answers (1)

Alberto Morillo
Alberto Morillo

Reputation: 15658

Repeat quotation marks or use CHAR(39) as explained on below thread:

Escaping single quote in SQL Server

Hope this helps.

Regards,

Alberto Morillo

Upvotes: 2

Related Questions