diff options
Diffstat (limited to 'Doc/includes/sqlite3/converter_point.py')
-rw-r--r-- | Doc/includes/sqlite3/converter_point.py | 21 |
1 files changed, 7 insertions, 14 deletions
diff --git a/Doc/includes/sqlite3/converter_point.py b/Doc/includes/sqlite3/converter_point.py index 5df828e336..147807a222 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') |