python to mssql encoding problem

无人久伴 提交于 2019-12-11 15:58:30

问题


Greetings

By using pymssql library, I want to write data to a MSSQL database however I encounter encoding issues. Here is my sample code to write to the DB:

# -*- coding: utf-8 -*-
import _mssql

....
Connection info data here
....


def mssql_connect():
    return _mssql.connect(server=HOST, user=USERNAME, password=PASS, database=DB, charset="utf-8")

con = mssql_connect()
INSERT_EX_SQL = "INSERT INTO myDatabsae (Id, ProgramName, ProgramDetail) VALUES (1, 'Test Characters ÜŞiçÇÖö', 'löşüIIğĞü');"
con.execute_non_query(INSERT_EX_SQL)
con.close()

Sadly the data that was written to DB is corrupted:

The Collacation of my mssql db is: Turkish_CI_AS How can this be solved?


回答1:


Here is a possible solution:

The key is INSERT_EX_SQ.encode('your language encoder'). Try this instead:

con.execute_non_query(INSERT_EX_SQ.encode('your language encoder'))


来源:https://stackoverflow.com/questions/4791114/python-to-mssql-encoding-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!