Created
October 17, 2022 17:40
-
-
Save ewalk153/1049dc3932e613c9db7836a90f706d1f to your computer and use it in GitHub Desktop.
Small script to demonstrate sqlite3 serialize/deserialize interface in python (>3.11rc1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requires python > 3.11rc1 for sqlite3.{serialize, deserialize} | |
import sqlite3 | |
def serialize(): | |
con = sqlite3.connect(":memory:") | |
cur = con.cursor() | |
cur.execute("CREATE TABLE movie(title, year, score)") | |
cur.execute(""" | |
INSERT INTO movie VALUES | |
('Monty Python and the Holy Grail', 1975, 8.2), | |
('And Now for Something Completely Different', 1971, 7.5) | |
""") | |
con.commit() | |
res = cur.execute("SELECT score FROM movie") | |
print(res.fetchall()) | |
bytes = con.serialize() | |
f = open("sample.db", "wb") | |
f.write(bytes) | |
f.close() | |
f = open("sample.db", "rb") | |
b2 = f.read() | |
f.close | |
cur.execute("DROP table movie") | |
con.commit() | |
con.deserialize(b2) | |
res = cur.execute("SELECT score FROM movie") | |
print(res.fetchall()) | |
serialize() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment