如何按名称选择列?(Python & SQLite3) [英] How do I select a column by its name? (Python & SQLite3)

查看:24
本文介绍了如何按名称选择列?(Python & SQLite3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有以下代码:

c.execute("SELECT * FROM table")
for row in c.fetchall():
    print row[0]
    print row[1]

但是,我更改了表的结构,现在我必须更改索引值以表示此更改.有没有办法改为使用列名?

However, I changed the structure of my table and now I have to change the index values to represent this change. Is there a way to get use column names instead?

推荐答案

参见 Row Objects 在 sqlite3 模块的文档中.如果你使用 sqlite3.Row row_factory 你会得到一个比普通元组稍微强大的对象.我想它的开销略高,因此不是默认行为.

See Row Objects in the docs for the sqlite3 module. If you use the sqlite3.Row row_factory you'll get back an object that's slightly more powerful than the normal tuples. I imagine it has slightly higher overhead, hence not being the default behavior.

这篇关于如何按名称选择列?(Python & SQLite3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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