Reputation: 91
The following is a screenshot of my code and database table data:
from sqlalchemy import Column, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mssql+pymssql://sa:12345678@XXXX:1433/YYYY')
Base = declarative_base(engine)
class User(Base):
__tablename__ = 'Products'
Id = Column(String(20), primary_key=True)
ProductName = Column(String(20))
ProductDesc = Column(String(50))
CreationTime = Column(String(20))
ProductCategory = Column(String(50))
def test():
db_session = sessionmaker(bind=engine)
session = db_session()
user = session.query(User).filter(User.Id == 5).all()
print(user)
========================= query results:[<main.User object at 0x7fd56b265400>]
I want it to return the specific values of all data that meet the filtering conditions. So,what went wrong?
This is the product table mapped above.
Upvotes: 1
Views: 1396
Reputation: 91
class BaseModel(object):
__abstract__ = True
def __init__(self):
self.__mapper__ = None
def __repr__(self):
fmt = u'[{}]'
attrs = (
(k, str(getattr(self, k)).replace(' ', '')) for k in self.__mapper__.columns.keys()
)
sattrs = ','.join('{}={!r}'.format(*x) for x in attrs)
return fmt.format(sattrs)
Base = declarative_base(cls=BaseModel)
Upvotes: 1