用法:
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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。