Python 3.4.1 插入 SQL Azure (pyodbc)

Har*_*ris 5 pyodbc azure-sql-database

我正在尝试将一些数据插入到在 SQL Azure 中创建的表中。

SQL结构

Field 1 DATE
Field 2 INT
Field 3 INT
Run Code Online (Sandbox Code Playgroud)

使用的Python代码:

#I know I have connected to the correct database.
Connection = pyodbc.connect(conn.conn()) 
cursor = Connection.cursor()

SQLCommand = ('INSERT INTO table_name ([Field 1], [Field 2], [Field 3]) VALUES ('31-Dec-14', 1, 2);')
cursor.execute(SQLCommand)
Connection.commit()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

pyodbc.ProgrammingError: ('42S22', "[42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '31-DEC-2014'. (207)
Run Code Online (Sandbox Code Playgroud)

如果我把它替换为

SQLCommand = ('INSERT INTO table_name ([Field 1], [Field 2], [Field 3]) VALUES (?, ?, ?);', ('31-DEC-2014',1,2))
cursor.execute(SQLCommand)
Connection.commit() 
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

TypeError: The first argument to execute must be a string or unicode query.
Run Code Online (Sandbox Code Playgroud)

我应该如何通过 python 将日期和整数输入到 SQL azure 表中?

谢谢

Tha*_*mer 1

日期解析器不喜欢您的格式。有关有效格式的列表,请参阅 Microsoft 文档

以下语法应该有效:

SQLCommand = ("INSERT INTO table_name ([Field 1], [Field 2], [Field 3]) VALUES ('2014-12-31', 1, 2);")
cursor.execute(SQLCommand)
Connection.commit()
Run Code Online (Sandbox Code Playgroud)