码迷,mamicode.com
首页 > 数据库 > 详细

python sqlite中通过字段名获取查询结果

时间:2016-06-11 17:25:13      阅读:824      评论:0      收藏:0      [点我收藏+]

标签:

在连sqlite数据库时,用fetchall()查询结果,是用row[0],row[1]这样的方式来打印每列的结果

但是我想用row[“字段名”]方式查询怎么办?

import sqlite3

con = sqlite3.connect("mydb")
con.row_factory = sqlite3.Row

cur = con.cursor()
cur.execute("select name_last, age from people")
for row in cur:
    assert row[0] == row["name_last"]
    assert row["name_last"] == row["nAmE_lAsT"]
    assert row[1] == row["age"]
    assert row[1] == row["AgE"]
#何问起 hovertree.com

 

MySQLdb的实现方法是: 
conn=MySQLdb.connect(..., cursorclass=MySQLdb.cursors.DictCursor) 
或者用MySQLdb.cursors.SSDictCursor。前者数据存储在客户端,后者数据存储在服务器。

推荐:http://www.cnblogs.com/roucheng/p/pythonstring.html

python sqlite中通过字段名获取查询结果

标签:

原文地址:http://www.cnblogs.com/roucheng/p/pythonziduan.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!