用法:
row_factory
您可以將此屬性更改為接受遊標和原始行作為元組的可調用對象,並將返回實際結果行。這樣,您可以實現更高級的返回結果的方式,例如返回一個也可以按名稱訪問列的對象。
例子:
import sqlite3 def dict_factory(cursor, row): d = {} for idx, col in enumerate(cursor.description): d[col[0]] = row[idx] return d con = sqlite3.connect(":memory:") con.row_factory = dict_factory cur = con.cursor() cur.execute("select 1 as a") print(cur.fetchone()["a"]) con.close()
如果返回元組還不夠,並且您希望基於名稱訪問列,則應考慮將
row_factory
設置為 highly-optimizedsqlite3.Row
類型。Row
提供對列的基於索引和不區分大小寫的基於名稱的訪問,幾乎沒有內存開銷。它可能比您自己的基於字典的自定義方法甚至基於db_row 的解決方案更好。
相關用法
- Python sqlite3.Connection.text_factory用法及代碼示例
- Python sqlite3.Connection.enable_load_extension用法及代碼示例
- Python sqlite3.Connection.iterdump用法及代碼示例
- Python sqlite3.Connection.create_collation用法及代碼示例
- Python sqlite3.Connection.create_function用法及代碼示例
- Python sqlite3.Connection.create_aggregate用法及代碼示例
- Python sqlite3.Connection.backup用法及代碼示例
- Python sqlite3.Cursor.executescript用法及代碼示例
- Python sqlite3.Cursor.connection用法及代碼示例
- Python sqlite3.Cursor.executemany用法及代碼示例
- Python sqlite3.threadsafety用法及代碼示例
- Python sqlite3.complete_statement用法及代碼示例
- Python sqlite3.connect用法及代碼示例
- Python math sqrt()用法及代碼示例
- Python sklearn.cluster.MiniBatchKMeans用法及代碼示例
- Python scipy.ndimage.binary_opening用法及代碼示例
- Python scipy.signal.windows.tukey用法及代碼示例
- Python scipy.stats.mood用法及代碼示例
- Python str.isidentifier用法及代碼示例
- Python sklearn.metrics.fbeta_score用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 sqlite3.Connection.row_factory。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。