0

Hi,
I am trying to push some data in sql server database using python 2.7.13 from ubuntu 14.04 LTS. I have one unicode object i.e. u'\U0001f381' in my data. Which is actually (u'\U0001f381') symbol. When I am trying to insert, it is giving me following error:

Python script:

    import pymssql
    conn = pymssql.connect(host=HOST, user=USER, password=PASSWORD, 
                    database=DATABASE)
    cursor = conn.cursor()
    field_lst = [u'2017-04-09', u'\U0001f381', 1.0, 0.0, 0.0, 3.0]
    placeholder = '%s,' * len(field_lst)
    query = 'INSERT INTO my_table VALUES (' + placeholder.strip(',') + ')'
    cursor.execute(query, tuple(field_lst))
    conn.commit()

Error:

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"),

When I tried to push same data from windows, using pycharm, it is getting inserted into database without any error. Any help would be appreciated. Thanks.

Subhash Deshmukh
  • 350
  • 3
  • 18
  • Why do you think this problem is related to Unicode or to that specific emoticon character? The error message is all about quotation marks. – lenz Jul 04 '17 at 23:11
  • @lenz Does that mean my string has some special character which I need to escape before inserting? When I ran above script it does not gave me any error. – Subhash Deshmukh Jul 05 '17 at 06:23
  • 1
    Use a profiler to see what query pymssql actually produces. Apparently it [doesn't believe in parameters](https://stackoverflow.com/questions/43980305) but instead substitutes values in query text, which is 1) a bad thing to do and 2) a difficult thing to do correctly with arbitrary Unicode strings and surrogates. Consider using Microsoft's own ODBC driver for SQL Server instead. – Jeroen Mostert Jul 05 '17 at 07:50

0 Answers0