我想使用 SQL Server 数据库中的 Python 创建一个数据字典。我已经编写了以下代码,但它给了我以下结果,
[OrderedDict([('SystemID', '1'), ('FileID', 1), ('DateTime', None)])]
但我希望我的结果如下,
File = { "SystemID": "1", "FileID": "1", "DateTime": None }
我现在有以下代码
import pyodbc
import collections
cnxn = pyodbc.connect(trusted_Connection='yes', driver = '{SQL Server}', server = 'localhost', database = 'myDB')
cursor = cnxn.cursor()
cursor.execute("SELECT SystemID, FileID, EventDateTime FROM dbo.File")
rows = cursor.fetchall()
objects_list = []
for row in rows:
d = collections.OrderedDict()
d['SystemID'] = row.SystemID
d['FileID'] = row.FileID
d['DateTime'] = row.EventDateTime
objects_list.append(d)
print objects_list
cnxn.close()