我正在尝试从SQL Server数据库中检索数据并使用python脚本转换为JSON格式,但出现一些错误。
这是我的代码:
import pyodbc
import json
conn = pyodbc.connect('Driver={SQL Server};'
'Server=localhost;'
'Database=TestDB;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('select * from dbo.test where id = 1 ')
rs = cursor.fetchall()
result = dict(rs)
r = json.dumps(rs)
for row in r:
print(r)
错误:
结果= dict(rs)
ValueError:字典更新序列元素#0的长度为6;需要2个
预期输出:
"{ "id" : 1, "type": "release", "consent": true, "quantity": 5}"
请让我知道我的脚本哪里写错了。