Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Sqlalchemy Define class from existing table

import sqlalchemy as sa

psw = "verysecret"
db = "accounts"

# create an engine
pengine = sa.create_engine('postgresql+psycopg2://postgres:' + psw +'@localhost/' + db)

from sqlalchemy.ext.declarative import declarative_base
# define declarative base
Base = declarative_base()

# reflect current database engine to metadata
metadata = sa.MetaData(pengine)
metadata.reflect()

# build your User class on existing `users` table
class User(Base):
    __table__ = sa.Table("users", metadata)
    
# call the session maker factory
Session = sa.orm.sessionmaker(pengine)
session = Session()

# filter a record 
session.query(User).filter(User.id==1).first()
Comment

PREVIOUS NEXT
Code Example
Python :: python get global variable by name 
Python :: choice without replacement python 
Python :: what is fn.call 
Python :: python timedelta get days with fraction 
Python :: unique list 
Python :: python % meaning 
Python :: pandas interpolate string 
Python :: software developer tools list 
Python :: python time a task 
Python :: plt grid linestyles options 
Python :: mathtext to regular python 
Python :: python docstrings example 
Python :: how to store something in python 
Python :: get output of a function in a variable python 
Python :: 2 functions at a time py 
Python :: how to create tupple in python 
Python :: Python colon equals 
Python :: how to define the range of values in seaborn heatmap 
Python :: how to divide string in python 
Python :: aiohttp specify app IP 
Python :: python c like struct 
Python :: make array consecutive 2 python 
Python :: toolbar pyqt 
Python :: export list to a file python 
Python :: python maximum product subarray 
Python :: python immutable dataclass 
Python :: how to set propee timeline in python 
Python :: python check if value in string 
Python :: python open zip file 
Python :: geopandas change column name 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =