Unable to send Unicode to SQL Server using pymssql

后端 未结 4 650
予麋鹿
予麋鹿 2021-01-14 04:46

I\'m having issues sending unicode to SQL Server via pymssql:

In [1]:     import pymssql
            conn = pymssql.connect(host=\'hostname\', user=\'me\', p         


        
4条回答
  •  [愿得一人]
    2021-01-14 05:21

    Here is something which worked for me:

    # -*- coding: utf-8 -*-
    import pymssql
    conn = pymssql.connect(host='hostname', user='me', password='password', database='db')
    cursor = conn.cursor()
    
    s = u'Monsieur le Curé of the «Notre-Dame-de-Grâce» neighborhood'
    
    cursor.execute("INSERT INTO MyTable(col1) VALUES(%s)", s.encode('latin-1', "ignore"))
    conn.commit()
    cursor.close()
    conn.close()
    

    MyTable is of collation: Latin1_General_CI_AS and the column col1 in it is of type varchar(MAX)

    My environment is: SQL Server 2008 & Python 2.7.10

提交回复
热议问题