如何显示SQLAlchemy正在执行哪些语句?

时间:2019-05-26 08:51:30

标签: python python-3.x sqlalchemy

我是SQLAlchemy的新手,我只希望SQLAlchemy打印要控制台的语句。 这是我的代码。

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, ForeignKey, Integer, String
engine = create_engine('sqlite:///')
Base = declarative_base()

class Person(Base):
    __tablename__ = 'person'
    id = Column(Integer, primary_key=True)
    name = Column(String(250), nullable=False)

class Address(Base):
    __tablename__ = 'address'
    id = Column(Integer, primary_key=True)
    street_name = Column(String(250))
    street_number = Column(String(250))
    post_code = Column(String(250), nullable=False)
    person_id = Column(Integer, ForeignKey('person.id'))
    person = relationship(Person)

Base.metadata.create_all(engine)

1 个答案:

答案 0 :(得分:1)

您必须在echo=True功能中设置create_engine

替换

engine = create_engine('sqlite:///')

使用

engine = create_engine('sqlite:///:memory:', echo=True)

reference

您还缺少一些进口商品。