Skip to content

Instantly share code, notes, and snippets.

@squeaky-pl
Created September 14, 2016 14:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squeaky-pl/097b8ecdd82c6b3ebce198fdfde88ec6 to your computer and use it in GitHub Desktop.
Save squeaky-pl/097b8ecdd82c6b3ebce198fdfde88ec6 to your computer and use it in GitHub Desktop.
In [1]: from sqlalchemy.ext.declarative import declarative_base
In [2]: Base = declarative_base()
In [3]: from sqlalchemy import Column, Integer, String
In [4]: class User(Base):
...: __tablename__ = 'users'
...: id = Column(Integer, primary_key=True)
...: name = Column(String)
...:
In [6]: from sqlalchemy.orm import Query
In [7]: q = Query([User.id, User.name]).filter(User.name.startswith('Ramón'))
In [14]: from sqlalchemy.dialects import mysql
In [16]: compiled = q.statement.compile(dialect=mysql.dialect())
In [18]: str(compiled)
Out[18]: "SELECT users.id, users.name \nFROM users \nWHERE (users.name LIKE concat(%s, '%%'))"
In [19]: compiled.params
Out[19]: {'name_1': 'Ramón'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment