91 lines
2.7 KiB
Python
91 lines
2.7 KiB
Python
|
"""
|
||
|
Consultar objetos de la Base de Datos
|
||
|
con ORM de SQLAlchemy
|
||
|
"""
|
||
|
from sqlalchemy import create_engine
|
||
|
from sqlalchemy.ext.declarative import declarative_base
|
||
|
from sqlalchemy import Column, Integer, String, Sequence
|
||
|
from sqlalchemy.orm import sessionmaker
|
||
|
from sqlalchemy.orm import aliased
|
||
|
|
||
|
|
||
|
engine = create_engine('sqlite:///:memory:')
|
||
|
Base = declarative_base()
|
||
|
|
||
|
class Author(Base):
|
||
|
__tablename__ = 'author'
|
||
|
|
||
|
id = Column(Integer, Sequence('author_id_seq'), primary_key=True)
|
||
|
firstname = Column(String)
|
||
|
lastname = Column(String)
|
||
|
|
||
|
def __repr__(self):
|
||
|
return "{} {}".format(self.firstname, self.lastname)
|
||
|
|
||
|
|
||
|
Base.metadata.create_all(engine)
|
||
|
author = Author(firstname="Juanita", lastname='Leon')
|
||
|
|
||
|
Session = sessionmaker(bind=engine)
|
||
|
session = Session()
|
||
|
session.add(author)
|
||
|
session.add_all([Author(firstname='Joél Ez', lastname='Silva'),
|
||
|
Author(firstname='Jorge', lastname='Olivares')])
|
||
|
session.commit()
|
||
|
|
||
|
"""
|
||
|
QUERYs
|
||
|
|
||
|
"""
|
||
|
print("Query #1:")
|
||
|
# Devuelve los Autores ordenados por ID.
|
||
|
for instance in session.query(Author).order_by(Author.id):
|
||
|
print(instance.firstname, instance.lastname)
|
||
|
|
||
|
print("Query #2:")
|
||
|
# Devuelve el Nombre y Apellido de cada Autor
|
||
|
for firstname, lastname in session.query(Author.firstname, Author.lastname):
|
||
|
print(firstname, lastname)
|
||
|
|
||
|
print("Query #3:")
|
||
|
# Devuelve el Autor, y su primer nombre
|
||
|
for row in session.query(Author, Author.firstname).all():
|
||
|
print(row.Author, row.firstname)
|
||
|
|
||
|
print("Query #4:")
|
||
|
# Devuelve los Autores, asignando una etiqueta al campo firsname
|
||
|
for row in session.query(Author.firstname.label('firstname_label')).all():
|
||
|
print(row.firstname_label)
|
||
|
|
||
|
print("Query #5:")
|
||
|
# Devuelve el Autor y su primer nombre, definiendo un alias de la tabla
|
||
|
author_alias = aliased(Author, name='author_alias')
|
||
|
for row in session.query(author_alias, author_alias.firstname).all():
|
||
|
print(row.author_alias)
|
||
|
|
||
|
print("Query #6:")
|
||
|
# Busqueda de todos los Autores, filtrados por slices, del 2 al 3
|
||
|
for an_author in session.query(Author).order_by(Author.id)[1:3]:
|
||
|
print(an_author)
|
||
|
|
||
|
print("Query #7:")
|
||
|
# Filtrado por autores con nombre 'Joél Ez'
|
||
|
for name, in session.query(Author.firstname).filter_by(firstname='Joél Ez'):
|
||
|
print(name)
|
||
|
|
||
|
print("Query #8:")
|
||
|
# Filtrado por apellido del Autor
|
||
|
for name, in session.query(Author.firstname).filter(Author.lastname == 'Silva'):
|
||
|
print(name)
|
||
|
|
||
|
print("Query #9:")
|
||
|
# Filtrado por nombre de autor y apellido
|
||
|
for an_author in session.query(Author).\
|
||
|
filter(Author.firstname == 'Jorge').\
|
||
|
filter(Author.lastname == 'Olivares'):
|
||
|
print(an_author)
|
||
|
|
||
|
print("Query #10")
|
||
|
# Cantidad de autores con el nombre 'Juanita'
|
||
|
print(session.query(Author).filter(Author.firstname == 'Juanita').count())
|