有没有办法获取 sqlite 中的列名称列表?
我想从数据库中的表中获取列名列表。使用 pragma 我得到了一个元组列表,其中包含很多不需要的信息。有没有办法只获取列名?所以我最终可能会得到这样的结果:
[列 1、列 2、列 3、列 4]
我绝对需要这个列表的原因是因为我想在列表中搜索列名并获取索引,因为索引在我的很多代码中使用。
有没有办法获得这样的列表?
谢谢
I want to get a list of column names from a table in a database. Using pragma I get a list of tuples with a lot of unneeded information. Is there a way to get only the column names? So I might end up with something like this:
[Column1, Column2, Column3, Column4]
The reason why I absolutely need this list is because I want to search for a column name in the list and get the index because the index is used in a lot of my code.
Is there a way of getting a list like this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
您可以使用 sqlite3 和 pep-249
cursor.description 是一个 7 项序列的序列,其第一个元素是列名称:
或者您可以使用列表理解:
You can use sqlite3 and pep-249
cursor.description is a sequence of 7-item sequences whose first element is the column name:
Alternatively you could use a list comprehension:
smallredstone 中的 cursor.description 解决方案的替代方案可能是使用 row。 keys():
缺点:它只有在查询至少返回一行时才有效。
好处:您可以通过名称访问列 (row['your_column_name'])
阅读有关 Python 文档中的行对象。
An alternative to the cursor.description solution from smallredstone could be to use row.keys():
The drawback: it only works if there is at least a row returned from the query.
The benefit: you can access the columns by their name (row['your_column_name'])
Read more about the Row objects in the python documentation.
据我所知,Sqlite 不支持 INFORMATION_SCHEMA。相反,它有 sqlite_master。
我不认为你可以通过一个命令获得你想要的列表。您可以使用 sql 或 pragma 获取所需的信息,然后使用正则表达式将其拆分为您需要的格式,
为您提供类似的信息
或者使用 pragma
为您提供类似的信息
As far as I can tell Sqlite doesn't support INFORMATION_SCHEMA. Instead it has sqlite_master.
I don't think you can get the list you want in just one command. You can get the information you need using sql or pragma, then use regex to split it into the format you need
gives you something like
Or using pragma
gives you something like
查看列名称的快速交互式方式
如果您在 Python 中进行交互工作并且只想快速“查看”列名称,我发现cursor.description 可以工作。
输出如下:
或者,快速访问和打印它们的方法。
输出如下:
Quick, interactive way to see column names
If you're working interactively in Python and just want to quickly 'see' the column names, I found cursor.description to work.
Outputs something like this:
Or, quick way to access and print them out.
Outputs something like this:
您可以通过运行来获取列名称列表:
您可以通过运行来检查某个列是否存在:
参考:
https://www.sqlite.org/pragma.html#pragfunc
You can get a list of column names by running:
You can check if a certain column exists by running:
Reference:
https://www.sqlite.org/pragma.html#pragfunc
这很容易。
首先创建一个连接,我们将其命名为
con
。然后运行以下代码。
您将获得列表形式的列名称
It is very easy.
First create a connection , lets name it,
con
.Then run the following code.
You will get column name as a list
假设您知道表名称,并且想要数据列的名称,您可以使用列出的代码将以一种简单而优雅的方式完成它,符合我的口味:
Assuming that you know the table name, and want the names of the data columns you can use the listed code will do it in a simple and elegant way to my taste:
好吧,我可能很晚才回答这个问题,但由于人们仍然关注这个线程,我只是想分享一下我如何使用
python sqlite3
获取列名列表。Well, I may be very late to answer this but since people still follow this thread, I just wanted to share how I use to get the list of column names in
python sqlite3
.我用这个:
I use this:
我喜欢@thebeancounter 的答案,但更喜欢参数化未知数,唯一的问题是表名上的漏洞。如果您确定没问题,那么这可行:
如果有问题,您可以添加代码来清理表名。
I like the answer by @thebeancounter, but prefer to parameterize the unknowns, the only problem being a vulnerability to exploits on the table name. If you're sure it's okay, then this works:
If it's a problem, you could add code to sanitize the tablename.
因为这个问题有一个 python 标志。我可以随意发布一个关于 pandas 的 python 特定答案:
Since the question has a python flag. I feel free to post a python specific answer with pandas:
使用 pragma 的另一种方法:
Another way of using pragma:
如果您喜欢使用pandas,我建议使用pandas.read_sql_query,它将返回一个完整的DataFrame,包括列名作为标题;这也很好地格式化为标准输出。
If you are fine with using
pandas
, I recommend usingpandas.read_sql_query
, which will give you back a full DataFrame including the column names as header; this also nicely formats to stdout.聚会有点晚了,但这就是我想出的解决方案小猪后退 @flokk 答案。
有效率吗?不,有效果吗?是的!
我试图解决的问题是我不知道表的列是什么,这对我帮助很大。
Kinda late to the party, but this is what i came up with this solution piggy backing off @flokk answer.
is it efficient? No. Does it work? Yes!
The problem I was trying to solve was that I had no clue what the columns of the table were, and this helped me out immensely.