gpt4 book ai didi

python - 有没有办法在 sqlite 中获取列名列表?

转载 作者:IT老高 更新时间:2023-10-28 20:38:24 36 4
gpt4 key购买 nike

我想从数据库中的表中获取列名列表。使用 pragma 我得到一个包含大量不需要信息的元组列表。有没有办法只获取列名?所以我可能会得到这样的结果:

[Column1, Column2, Column3, Column4]

之所以我绝对需要这个列表,是因为我想在列表中搜索一个列名并获取索引,因为我的很多代码中都使用了索引。

有没有办法得到这样的列表?

谢谢

最佳答案

您可以使用 sqlite3 和 pep-249

import sqlite3
connection = sqlite3.connect('~/foo.sqlite')
cursor = connection.execute('select * from bar')

cursor.description 是列的描述

names = list(map(lambda x: x[0], cursor.description))

您也可以使用列表推导:

names = [description[0] for description in cursor.description]

关于python - 有没有办法在 sqlite 中获取列名列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7831371/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com