python-pyodbc和openjson问题

fzwojiic  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(317)

我试图在我的python脚本中运行以下代码,但由于语法错误而失败,我无法纠正。第一个insert从一个web站点引入json数据并完美地工作。第二条insert语句格式化json代码并将其放入另一个sql表中。这段代码在mssql中运行良好。第二个插入是指缺少结束引号,但我似乎有一个开始和结束引号。它还引用未解析的引用openjson和未解析的引用交叉应用。我试过在insert和select语句周围加上左括号和右括号,但都没有成功。我查看了github上的pyodbc wiki,但没有看到类似的示例。我做错什么了?谢谢。

cursor.execute("Insert Into InboundJson (json) values (?)", (json.dumps(response_json),))
conn.commit()  -- works fine

cursor = conn.cursor()
cursor.execute("Insert into [CCSBC-DW1].[SurveyMonkey].[dbo].Surveylist (SurveyId, Description, WebSite)

Select J_open.*
from
  dbo.InboundJson j
  cross apply
  openjson(j.[json], '$.data') with ( id          int ,
                                      title  varchar(200),

                                      href    varchar(200)) j_open;")
conn.commit()```
hmtdttj4

hmtdttj41#

对于多行字符串,只需使用三重引号:

cursor.execute("""INSERT INTO [CCSBC-DW1].[SurveyMonkey].[dbo].Surveylist (SurveyId, Description, WebSite)
                  SELECT J_open.*
                  FROM dbo.InboundJson j
                  CROSS APPLY OPENJSON(j.[json], '$.data') 
                              WITH (id int,
                                    title varchar(200),
                                    href varchar(200)) j_open;
               """)
conn.commit()

相关问题