Search This Blog

Featured Post

Machine Learning, Big Data, AI, Deep Learning

Monday, April 23, 2018

24/4/2018 SQLAlchemy


Flow of building connection with database via SQLAlchemy

Create Engine


Step

Key Code

Inspect

Connecting
>>> from sqlalchemy import create_engine
>>> engine = create_engine('sqlite:///:memory:', echo=True)
Map Declare
>>> from sqlalchemy.ext.declarative import declarative_base
>>> Base = declarative_base()
>>> from sqlalchemy import Column, Integer, String
>>> class User(Base):
...     __tablename__ = 'users'
...
...     id = Column(Integer, primary_key=True)
...     name = Column(String)
...     fullname = Column(String)
...     password = Column(String)
...
...     def __repr__(self):
...        return "<User(name='%s', fullname='%s', password='%s')>" % (
...                             self.name, self.fullname, self.password)
>>> User.__table__ 
Table('users', MetaData(bind=None),
  Column('id', Integer(), table=<users>, primary_key=True, nullable=False),
  Column('name', String(), table=<users>),
  Column('fullname', String(), table=<users>),
  Column('password', String(), table=<users>), schema=None)

No comments:

Post a Comment