我在python 2.5中使用sqlite3.我创建了一个如下所示的表:
create table votes (
bill text,
senator_id text,
vote text)
Run Code Online (Sandbox Code Playgroud)
我用这样的东西访问它:
v_cur.execute("select * from votes")
row = v_cur.fetchone()
bill = row[0]
senator_id = row[1]
vote = row[2]
Run Code Online (Sandbox Code Playgroud)
我希望能够做的是让fetchone(或其他一些方法)返回一个字典,而不是一个列表,这样我就可以通过名称而不是位置来引用字段.例如:
bill = row['bill']
senator_id = row['senator_id']
vote = row['vote']
Run Code Online (Sandbox Code Playgroud)
我知道你可以用MySQL做到这一点,但是有谁知道如何用SQLite做到这一点?
谢谢!!!
当我做某事的时候
sqlite.cursor.execute("SELECT * FROM foo")
result = sqlite.cursor.fetchone()
Run Code Online (Sandbox Code Playgroud)
我认为必须记住列似乎能够取出它们的顺序,例如
result[0] is id
result[1] is first_name
Run Code Online (Sandbox Code Playgroud)
有没有办法归还字典?所以我可以只使用结果['id']或类似的?
编号列的问题是,如果您编写代码然后插入一个列,您可能需要更改代码,例如first_name的result [1]现在可能是date_joined,因此必须更新所有代码...
我想使用此代码从sqlite数据库列读取所有温度值,但输出显示[(u'29',), (u'29',), (u'29',)],我只是将数值存储在数据库中.我希望输出[29, 29, 29]
import sqlite3
conn = sqlite3.connect("growll.db")
cursor = conn.cursor()
print "\nHere's a listing of all the records in the table:\n"
cursor.execute("select lchar from GrowLLDados")
print cursor.fetchall()
Run Code Online (Sandbox Code Playgroud) 我在ms-access db中有一个表,我需要只获取该表的列名,列表或元组.
如果有人可以建议查询或指向我的文档.