-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlalchemy-orm.py
More file actions
37 lines (27 loc) · 955 Bytes
/
Copy pathsqlalchemy-orm.py
File metadata and controls
37 lines (27 loc) · 955 Bytes
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
username, password, database, host, port = "psql1", "pwd1", "psql1", "localhost", 5432
# docker run -d --name psql1 -e POSTGRES_USER=psql1 -e POSTGRES_PASSWORD=pwd1 -e POSTGRES_DB=psql1 -p 5432:5432 postgres
# #
# python -m pip install sqlalchemy psycopg2
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import declarative_base, sessionmaker
# Database connection
engine = create_engine(f"postgresql+psycopg2://{username}:{password}@{host}:{port}/{database}")
Base = declarative_base()
# Define a model
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String)
# Create tables
Base.metadata.create_all(engine)
# Session
Session = sessionmaker(bind=engine)
session = Session()
# Add a user
new_user = User(name="Bob")
session.add(new_user)
session.commit()
# Query users
for user in session.query(User).all():
print("id and name")
print(user.id, user.name)