-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres-test.py
More file actions
24 lines (19 loc) · 1.08 KB
/
postgres-test.py
File metadata and controls
24 lines (19 loc) · 1.08 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
# Import SQLAlchemy's create_engine function
from sqlalchemy import create_engine, text
# Create a connection string
# [Python postgres drivers](https://wiki.postgresql.org/wiki/Python)
# most popular 'psycopg2' pip install fails with MS Windows Python
# engine = create_engine("postgresql+psycopg2://admin:admin@localhost:5432/test")
engine = create_engine("postgresql+pg8000://admin:admin@localhost:5432/test")
# Test the connection
# connection = engine.connect()
# print("Connected to PostgreSQL database successfully!")
# connection.close()
with engine.connect() as connection:
connection.execute(text("CREATE TABLE example (id SERIAL PRIMARY KEY, name VARCHAR(20) NOT NULL)"))
connection.execute(text("INSERT INTO example (name) VALUES (:name)"), {"name": "Ashley"})
connection.execute(text("INSERT INTO example (name) VALUES (:name)"), [{"name": "Barry"}, {"name": "Christina"}])
connection.commit()
result = connection.execute(text("SELECT * FROM example WHERE name = :name"), dict(name="Ashley"))
for row in result.mappings():
print("Author:", row["name"])