我需要列表中sql表的列名才能在另一个复杂的python代码中使用 尝试了一些较旧的SO帖子中显示的一些黑客:
db_columns = cursor.columns(table='result', schema='dbo').fetchall()
[(u'Basis', u'dbo', u'result', u'TXN_KEY', -5, u'bigint identity', 19, 8, 0, 10, 0, None, None, -5, None, None, 1, 'NO', 0, 0, 0, 1, None, None, None, None, None, None, 63),(u'Basis', u'dbo', u'result', u'Send_Agent', 12, u'varchar', 9, 9, None, None, 1, None, None, 12, None, 9, 2, 'YES', 0, 0, 0, 0, None, None, None, None, None, None, 39),(u'Basis', u'dbo', u'result', u'Pay_Agent', 12, u'varchar', 9, 9, None, None, 1, None, None, 12, None, 9, 3, 'YES', 0, 0, 0, 0, None, None, None, None, None, None, 39)]
上面的输出似乎乱码,有很多不必要的信息。我所需要的只是结果表的列名并将其分配给列表 = ['TXN_KEY','Send_Agent','Pay_Agent']
for row in cursor.columns(table='result'):
x = row.column_name
x
Out[14]: u'Pay_Agent'
此输出仅给我一个列名。
提前感谢您的见解。
SELECT
这样吗?
SELECT COLUMN_NAME,*
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='dbo' and TABLE_NAME = 'YourTableName'