microsoft / microsoft/mssql-python

Error when selecting data from table with JSON datatype

オープン
#261 コメント 2 件 リアクション 1 件 担当者 1 名 GitHub で見る

@gargsaumya がすでに取り組んでいます。

2025年9月26日 から。

area: data-types triage done
主要言語
Python
スター
472
フォーク
60
平均マージ
2日 11時間
マージ済み PR(30日)
36

説明

Describe the bug

When calling cursor.execute to select a json column from a table you receive a runtime error RuntimeError: [Microsoft][ODBC Driver 18 for SQL Server]The connection is no longer usable because the server response for a previously executed statement was incorrectly formatted.

If you are seeing an exception, include the full exceptions details (message and stack trace).

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[13], [line 1](vscode-notebook-cell:?execution_count=13&line=1)
----> [1](vscode-notebook-cell:?execution_count=13&line=1) with connect(getenv("SQL_CONNECTION_STRING")) as conn: # type: ignore
      2     with conn.cursor() as cursor:
      3         cursor.execute("select * from SalesLT.CustomerJson")

File c:\python\notebook\.venv\Lib\site-packages\mssql_python\connection.py:1028, in Connection.__exit__(self, *args)
   1021 """
   1022 Exit the context manager.
   1023 
   1024 Closes the connection when exiting the context, ensuring proper resource cleanup.
   1025 This follows the modern standard used by most database libraries.
   1026 """
   1027 if not self._closed:
-> [1028](file:///C:/python/notebook/.venv/Lib/site-packages/mssql_python/connection.py:1028)     self.close()

File c:\python\notebook\.venv\Lib\site-packages\mssql_python\connection.py:971, in Connection.close(self)
    966 if not self.autocommit:
    967     # If autocommit is disabled, rollback any uncommitted changes
    968     # This is important to ensure no partial transactions remain
    969     # For autocommit True, this is not necessary as each statement is committed immediately
    970     log('info', "Rolling back uncommitted changes before closing connection.")
--> [971](file:///C:/python/notebook/.venv/Lib/site-packages/mssql_python/connection.py:971)     self._conn.rollback()
    972 # TODO: Check potential race conditions in case of multithreaded scenarios
    973 # Close the connection
    974 self._conn.close()

RuntimeError: [Microsoft][ODBC Driver 18 for SQL Server]The connection is no longer usable because the server response for a previously executed statement was incorrectly formatted.
To reproduce

Include a complete code listing (or project/solution) that we can run to reproduce the issue.

Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.

Run the following tsql in a database with the AdventureWorksLT sample data loaded.

select *
from (SELECT * 
  FROM [SalesLT].[Customer]
  for json AUTO) dt

  create table SalesLT.CustomerJson
    (
        CustomerID int primary key,
        CustomerJson JSON
    )

    insert into SalesLT.CustomerJson (CustomerID, CustomerJson)
    select 1, (SELECT * 
      FROM [SalesLT].[Customer]
      for json AUTO)
with connect(getenv("SQL_CONNECTION_STRING")) as conn: # type: ignore
    with conn.cursor() as cursor:
        cursor.execute("select * from SalesLT.CustomerJson")
        if cursor:
            row = cursor.fetchone()
            if row:
                print(row.CustomerJson)
Expected behavior

A string is returned that is in JSON or convertible to JSON.

Further technical details

Python version: 3.12
SQL Server version: SQL database in Fabric
Operating system: uv

Additional context
Can share uv folder directly since it includes connection strings.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。