用法:
text_factory
使用此屬性,您可以控製為
TEXT
數據類型返回的對象。默認情況下,此屬性設置為str
並且sqlite3
模塊將為TEXT
返回str
對象。如果您想返回bytes
,可以將其設置為bytes
。您還可以將其設置為任何其他接受單個字節字符串參數並返回結果對象的可調用對象。
請參閱以下示例代碼進行說明:
import sqlite3 con = sqlite3.connect(":memory:") cur = con.cursor() AUSTRIA = "Österreich" # by default, rows are returned as str cur.execute("select ?", (AUSTRIA,)) row = cur.fetchone() assert row[0] == AUSTRIA # but we can make sqlite3 always return bytestrings ... con.text_factory = bytes cur.execute("select ?", (AUSTRIA,)) row = cur.fetchone() assert type(row[0]) is bytes # the bytestrings will be encoded in UTF-8, unless you stored garbage in the # database ... assert row[0] == AUSTRIA.encode("utf-8") # we can also implement a custom text_factory ... # here we implement one that appends "foo" to all strings con.text_factory = lambda x: x.decode("utf-8") + "foo" cur.execute("select ?", ("bar",)) row = cur.fetchone() assert row[0] == "barfoo" con.close()
相關用法
- Python sqlite3.Connection.enable_load_extension用法及代碼示例
- Python sqlite3.Connection.iterdump用法及代碼示例
- Python sqlite3.Connection.create_collation用法及代碼示例
- Python sqlite3.Connection.row_factory用法及代碼示例
- 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.text_factory。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。