python sqlite中通過欄位名擷取查詢結果

來源:互聯網
上載者:User

標籤:

在連sqlite資料庫時,用fetchall()查詢結果,是用row[0],row[1]這樣的方式來列印每列的結果

但是我想用row[“欄位名”]方式查詢怎麼辦?

import sqlite3con = sqlite3.connect("mydb")con.row_factory = sqlite3.Rowcur = 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中通過欄位名擷取查詢結果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.