diff options
author | Erlend Egeberg Aasland <erlend.aasland@protonmail.com> | 2022-06-25 20:06:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-25 20:06:06 (GMT) |
commit | bd3c1c187e0e4fde5aec6835d180e9eddde8ceb6 (patch) | |
tree | 8030a0312783066d3cfeaae482bc3755b5dff9fe /Doc/includes | |
parent | bec802dbb87717a23acb9c600c15f40bc98340a3 (diff) | |
download | cpython-bd3c1c187e0e4fde5aec6835d180e9eddde8ceb6.zip cpython-bd3c1c187e0e4fde5aec6835d180e9eddde8ceb6.tar.gz cpython-bd3c1c187e0e4fde5aec6835d180e9eddde8ceb6.tar.bz2 |
gh-90016: Reword sqlite3 adapter/converter docs (#93095)
Also add adapters and converter recipes.
Co-authored-by: CAM Gerlach <CAM.Gerlach@Gerlach.CAM>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com
Diffstat (limited to 'Doc/includes')
-rw-r--r-- | Doc/includes/sqlite3/adapter_datetime.py | 17 | ||||
-rw-r--r-- | Doc/includes/sqlite3/converter_point.py | 21 |
2 files changed, 7 insertions, 31 deletions
diff --git a/Doc/includes/sqlite3/adapter_datetime.py b/Doc/includes/sqlite3/adapter_datetime.py deleted file mode 100644 index d5221d8..0000000 --- a/Doc/includes/sqlite3/adapter_datetime.py +++ /dev/null @@ -1,17 +0,0 @@ -import sqlite3 -import datetime -import time - -def adapt_datetime(ts): - return time.mktime(ts.timetuple()) - -sqlite3.register_adapter(datetime.datetime, adapt_datetime) - -con = sqlite3.connect(":memory:") -cur = con.cursor() - -now = datetime.datetime.now() -cur.execute("select ?", (now,)) -print(cur.fetchone()[0]) - -con.close() diff --git a/Doc/includes/sqlite3/converter_point.py b/Doc/includes/sqlite3/converter_point.py index 5df828e..147807a 100644 --- a/Doc/includes/sqlite3/converter_point.py +++ b/Doc/includes/sqlite3/converter_point.py @@ -5,28 +5,23 @@ class Point: self.x, self.y = x, y def __repr__(self): - return "(%f;%f)" % (self.x, self.y) + return f"Point({self.x}, {self.y})" def adapt_point(point): - return ("%f;%f" % (point.x, point.y)).encode('ascii') + return f"{point.x};{point.y}".encode("utf-8") def convert_point(s): x, y = list(map(float, s.split(b";"))) return Point(x, y) -# Register the adapter +# Register the adapter and converter sqlite3.register_adapter(Point, adapt_point) - -# Register the converter sqlite3.register_converter("point", convert_point) +# 1) Parse using declared types p = Point(4.0, -3.2) - -######################### -# 1) Using declared types con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) -cur = con.cursor() -cur.execute("create table test(p point)") +cur = con.execute("create table test(p point)") cur.execute("insert into test(p) values (?)", (p,)) cur.execute("select p from test") @@ -34,11 +29,9 @@ print("with declared types:", cur.fetchone()[0]) cur.close() con.close() -####################### -# 1) Using column names +# 2) Parse using column names con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_COLNAMES) -cur = con.cursor() -cur.execute("create table test(p)") +cur = con.execute("create table test(p)") cur.execute("insert into test(p) values (?)", (p,)) cur.execute('select p as "p [point]" from test') |