-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example showing prepared statements with pg8000 (#821)
Example showing prepared statements with pg8000
- Loading branch information
1 parent
8c31d35
commit fcc4237
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
.. highlight: console | ||
################################################# | ||
Connect to CrateDB and CrateDB Cloud using pg8000 | ||
################################################# | ||
|
||
|
||
***** | ||
About | ||
***** | ||
|
||
Example programs demonstrating CrateDB with pg8000, | ||
for the PostgreSQL protocol. | ||
|
||
|
||
***** | ||
Setup | ||
***** | ||
|
||
To start a CrateDB instance on your machine for evaluation purposes, invoke:: | ||
|
||
docker run --publish 4200:4200 --publish 5432:5432 --env CRATE_HEAP_SIZE=1g crate:latest -Cdiscovery.type=single-node | ||
|
||
Navigate to the example program directory, and install prerequisites:: | ||
|
||
# Acquire sources. | ||
git clone https://github.com/crate/cratedb-examples | ||
cd cratedb-examples | ||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
pip install pg8000 | ||
|
||
|
||
******** | ||
Examples | ||
******** | ||
|
||
Run an example program:: | ||
|
||
python basic.py | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import ssl | ||
|
||
import pg8000 | ||
|
||
# When connecting to CrateDB Cloud you may want to use SSL | ||
# ssl_context = ssl.create_default_context() | ||
|
||
conn = pg8000.connect( | ||
user="crate", | ||
password="", | ||
host="localhost", | ||
port=5432, | ||
database="doc", | ||
# ssl_context=ssl_context, | ||
) | ||
|
||
query = """ | ||
SELECT mountain,height | ||
FROM sys.summits | ||
WHERE height >= :minimum_height | ||
ORDER BY height DESC | ||
LIMIT :number_of_rows; | ||
""" | ||
|
||
ps = conn.prepare(query) | ||
ps.run(minimum_height=4000, number_of_rows=10) |