python sqlite中通過字段名獲取查詢結果


在連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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM