-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.sql
More file actions
71 lines (50 loc) · 2.02 KB
/
sqlite.sql
File metadata and controls
71 lines (50 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
-- Store and retrieve small SQLite databases inside PostgreSQL
--
-- This PL/Python snippet demonstrates how to serialize an in-memory SQLite
-- database into a PostgreSQL table (BYTEA column) and then deserialize it
-- back into SQLite.
--
-- Notes:
-- - Uses Python's standard library (sqlite3), not available in plain SQL.
-- - Intended for experimentation, debugging, or fun projects.
-- - Uses an UNLOGGED table for temporary storage (faster, avoids WAL).
-- - Transaction-safe: can be run inside BEGIN/ROLLBACK without side effects.
-- - Not suitable for large databases or production storage.
--
-- Usage examples (from `pgsql` shell):
-- pgawesome=# \i /path/to/sqlite.sql
--
BEGIN;
CREATE UNLOGGED TABLE IF NOT EXISTS sqlites (
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
sqlite BYTEA
);
DO $$
import sqlite3
import plpy
# create an in-memory SQLite database with schema and data
conn = sqlite3.connect(':memory:')
conn.execute("CREATE TABLE IF NOT EXISTS plpy_awesome (id INTEGER PRIMARY KEY, comment TEXT)")
conn.execute("INSERT INTO plpy_awesome(comment) VALUES ('PL/Python is awesome'), ('As is PostgreSQL')")
conn.commit()
# check inserted data
plpy.info(conn.execute("SELECT * FROM plpy_awesome").fetchall())
# serialize in-memory SQLite into a PostgreSQL table
plan = plpy.prepare('INSERT INTO sqlites (sqlite) VALUES ($1)', ['bytea'])
plpy.execute(plan, ([conn.serialize()]))
# check serialization result
plpy.info(plpy.execute('SELECT id, LENGTH(sqlite) FROM sqlites LIMIT 1', 1)[0])
conn.close()
$$ LANGUAGE 'plpython3u';
DO $$
import sqlite3
import plpy
# create an in-memory SQLite database
conn = sqlite3.connect(':memory:')
# deserialize and load SQLite database stored in a PostgreSQL table
conn.deserialize(plpy.execute('SELECT sqlite FROM sqlites LIMIT 1', 1)[0]['sqlite'])
# check the result
plpy.info(conn.execute("SELECT * FROM plpy_awesome").fetchall())
conn.close()
$$ LANGUAGE 'plpython3u';
ROLLBACK;