-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-update_valentines.py
31 lines (24 loc) · 1.04 KB
/
4-update_valentines.py
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
from sqlalchemy import create_engine, Column, String
from sqlalchemy.orm import sessionmaker, declarative_base
username = 'user'
password = 'password'
host = 'mysql'
port = '3306'
database = 'mydatabase'
# Create the SQLAlchemy engine with echo enabled for logging SQL statements
engine = create_engine(f'mysql+pymysql://{username}:{password}@{host}:{port}/{database}', echo=True)
# Define the ORM model
Base = declarative_base()
class Valentine(Base):
__tablename__ = 'valentines'
name = Column(String(50), primary_key=True)
message = Column(String(256))
# Create a session
Session = sessionmaker(bind=engine)
session = Session()
# Query and update the message for Robin's valentine
robin_valentine = session.query(Valentine).filter_by(name='Robin').first()
robin_valentine.message = "I found out recently that I love you, after a near death experience. It is still meaningful despite, my love. Ignore the circumstances. I hope you love me too. If not, please return the gifts."
# Commit the session and close it
session.commit()
session.close()