Writing raw SQL queries

If you need to select entities by a raw SQL query, you can do it this way:

  1. >>> x = 25
  2. >>> Person.select_by_sql('SELECT * FROM Person p WHERE p.age < $x')
  3. SELECT * FROM Person p WHERE p.age < ?
  4. [25]
  5. [Person[1], Person[2]]

If you want to work with the database directly, avoiding entities, you can use the Database.select() method:

  1. >>> x = 20
  2. >>> db.select('name FROM Person WHERE age > $x')
  3. SELECT name FROM Person WHERE age > ?
  4. [20]
  5. [u'Mary', u'Bob']