Skip to content

Commit fcc4237

Browse files
authored
Example showing prepared statements with pg8000 (#821)
Example showing prepared statements with pg8000
1 parent 8c31d35 commit fcc4237

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

by-language/python-pg8000/README.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.. highlight: console
2+
3+
#################################################
4+
Connect to CrateDB and CrateDB Cloud using pg8000
5+
#################################################
6+
7+
8+
*****
9+
About
10+
*****
11+
12+
Example programs demonstrating CrateDB with pg8000,
13+
for the PostgreSQL protocol.
14+
15+
16+
*****
17+
Setup
18+
*****
19+
20+
To start a CrateDB instance on your machine for evaluation purposes, invoke::
21+
22+
docker run --publish 4200:4200 --publish 5432:5432 --env CRATE_HEAP_SIZE=1g crate:latest -Cdiscovery.type=single-node
23+
24+
Navigate to the example program directory, and install prerequisites::
25+
26+
# Acquire sources.
27+
git clone https://github.com/crate/cratedb-examples
28+
cd cratedb-examples
29+
python3 -m venv .venv
30+
source .venv/bin/activate
31+
pip install pg8000
32+
33+
34+
********
35+
Examples
36+
********
37+
38+
Run an example program::
39+
40+
python basic.py
41+

by-language/python-pg8000/basic.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import ssl
2+
3+
import pg8000
4+
5+
# When connecting to CrateDB Cloud you may want to use SSL
6+
# ssl_context = ssl.create_default_context()
7+
8+
conn = pg8000.connect(
9+
user="crate",
10+
password="",
11+
host="localhost",
12+
port=5432,
13+
database="doc",
14+
# ssl_context=ssl_context,
15+
)
16+
17+
query = """
18+
SELECT mountain,height
19+
FROM sys.summits
20+
WHERE height >= :minimum_height
21+
ORDER BY height DESC
22+
LIMIT :number_of_rows;
23+
"""
24+
25+
ps = conn.prepare(query)
26+
ps.run(minimum_height=4000, number_of_rows=10)

0 commit comments

Comments
 (0)