【问题标题】:Creating table in MSSQL via pyodbc module in Python通过 Python 中的 pyodbc 模块在 MSSQL 中创建表
【发布时间】:2021-03-16 15:11:10
【问题描述】:

我正在尝试在我的 MSSQL 中创建此表

import pyodbc


conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=LAPTOP;'
                      'Database=DB;'
                      'Trusted_Connection=yes;')

cursor = conn.cursor()

cursor.execute('''
              CREATE TABLE logging.t_history (
              id serial,
              tstamp timestamp DEFAULT now(),
              schemaname text,
              tabname text,
              operation text,
              who text DEFAULT current_user,
              new_val json,
              old_val json
              )
               ''')

conn.commit() cursor = conn.cursor()

但我得到了错误 -

pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Column, parameter, or variable #7: Cannot find data type json. (2715) (SQLExecDirectW)')

这也发生在时间戳为“DEFAUT now()”的情况下。我该如何解决这个问题?

【问题讨论】:

标签: sql-server database pyodbc


【解决方案1】:

Json 不是 sql server 中的数据类型,您使用 varchar(max) 数据类型将 json 存储为文本。

编辑 事实上你标记了正确的数据库平台,你的创建表看起来不像 ms-sqlserver。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-07
相关资源
最近更新 更多