有没有办法得到列名称的列表在sqlite? [英] Is there a way to get a list of column names in sqlite?

查看:162
本文介绍了有没有办法得到列名称的列表在sqlite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


[Column1,Column2,Column3,Column4] / blockquote>

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



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



谢谢

解决方案

您可以使用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 cursor.description] 


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

解决方案

You can use sqlite3 and pep-249

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

cursor.description is description of columns

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

Alternatively you could use a list comprehension:

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

这篇关于有没有办法得到列名称的列表在sqlite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆