【发布时间】:2021-02-21 23:29:14
【问题描述】:
我正在使用 SQLAlchemy(使用 pymssql 驱动程序)连接到 SQL Server 数据库。
import sqlalchemy
conn_string = f'mssql+pymssql://{uid}:{pwd}@{instance}/?database={db};charset=utf8'
sql = 'SELECT * FROM FAKETABLE;'
engine = sqlalchemy.create_engine(conn_string)
connection = engine.connect()
result = connection.execute(sql)
result.cursor.description
导致:
(('col_1', 1, None, None, None, None, None),
('col_2', 1, None, None, None, None, None),
('col_3', 4, None, None, None, None, None),
('col_4', 3, None, None, None, None, None),
('col_5', 3, None, None, None, None, None))
根据PEP 249(光标的.description属性):
前两项(name 和 type_code)是必填项,其他五项是可选的,如果无法提供有意义的值,则设置为 None。
我假设整数 (1, 1, 4, 3, 3) 是列类型。
我的两个问题:
- 如何将这些整数映射到数据类型(如 char、integer 等)?
- 这些是 SQL 数据类型吗?如果没有,是否可以获取 SQL 数据类型?
FWIW,当我使用 raw_connection() 而不是 connect() 时,我得到了相同的结果。
遇到了三个类似的问题(不回答这个特定问题)。我需要使用connect() + execute() 方法。
【问题讨论】:
标签: python sql-server sqlalchemy pymssql