Insert special character ° using python and mssql

时间:2016-02-12 19:39:41

标签: python mysql sql sql-server pyodbc

In Python and MSSQL, using pyodc Im trying to create a column in the table with the name Azimuth °, but when Im create the line :

cmd = (" CREATE TABLE " + tableName + "_fly" +" ("
" [Azimuth (°)] float,"
") ")

the table its created but with this name in the header: Azimuth (°)

any suggestions?

thanks in advance

2 个答案:

答案 0 :(得分:2)

更新(2019年2月):这似乎不再是pyodbc的问题,至少在Python_3下。 pyodbc 4.0.25使用正确的列名创建表。

<小时/> (原始答案)

即使将utf-8指定为源代码编码,我也可以使用pyodbc重现您的问题。

但是,我确实使用pymssql得到了正确的结果:

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

cnxn = pymssql.connect(
    server='localhost',
    port='52865',
    user='sa',
    password='whatever',
    database='myDb',
    autocommit=True)
crsr = cnxn.cursor()

sql = """\
CREATE TABLE tableName_fly (
    [ID] INT IDENTITY PRIMARY KEY,
    [Azimuth (°)] FLOAT
    )
"""
crsr.execute(sql)

crsr.close()
cnxn.close()

答案 1 :(得分:1)

This doesn't answer your question, but it might be helpful to know that the Azimuth character ° is a supported identifier in MSSQL.

The rules for valid database identifiers is mentioned here, you can select your matching DB version. The Azimuth character is a Unicode character, part of the 3.2 standard library under the Latin-1_Supplement block.