用法:
create_collation(name, callable)
使用指定的
name
和callable
創建一個排序規則。可調用對象將傳遞兩個字符串參數。如果第一個排序低於第二個,它應該返回 -1,如果它們排序相等,則返回 0,如果第一個排序高於第二個,則返回 1。請注意,這控製排序(SQL 中的 ORDER BY),因此您的比較不會影響其他 SQL 操作。請注意,可調用對象將以 Python 字節串的形式獲取其參數,通常以 UTF-8 編碼。
以下示例顯示了對 “the wrong way” 進行排序的自定義排序規則:
import sqlite3 def collate_reverse(string1, string2): if string1 == string2: return 0 elif string1 < string2: return 1 else: return -1 con = sqlite3.connect(":memory:") con.create_collation("reverse", collate_reverse) cur = con.cursor() cur.execute("create table test(x)") cur.executemany("insert into test(x) values (?)", [("a",), ("b",)]) cur.execute("select x from test order by x collate reverse") for row in cur: print(row) con.close()
要刪除排序規則,請調用
create_collation
並將None
作為可調用對象:con.create_collation("reverse", None)
相關用法
- Python sqlite3.Connection.create_function用法及代碼示例
- Python sqlite3.Connection.create_aggregate用法及代碼示例
- Python sqlite3.Connection.text_factory用法及代碼示例
- Python sqlite3.Connection.enable_load_extension用法及代碼示例
- Python sqlite3.Connection.iterdump用法及代碼示例
- Python sqlite3.Connection.row_factory用法及代碼示例
- 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.create_collation。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。