Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Using Databases in Python Meet Peewee Queries Are Your Friend

Is there an advantage to the Peewee syntax over standard SQL?

If I had a choice, I'd prefer a database library that connects to SQLite or what have you and has an execute() method that you feed standard SQL statements. I just find the Peewee syntax cumbersome and convoluted and imagine that in a business context knowledge of ANSI SQL statement construction and usage would be preferable.

2 Answers

I'm the author of peewee so I'm of course biased, but just wanted to say that Peewee queries generally tend to be quite close to their SQL analogues. Compare them to Django and you'll see what I mean. Yes, for simple queries it can be more characters, but using an ORM like peewee has quite a few benefits:

  • Re-use query fragments
  • Build queries programmatically, piece by piece without nasty string munging
  • Leverage python's parser to ensure your queries are valid (i.e., it's near impossible to forget a parentheses, omit a comma, etc).
  • Safe against SQL injection
  • Probably other stuff, too.

IMO, I think PeeWee's readability, from someone with little exposure to many SQL queries, helps as well.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I don't know any professional Python (or Ruby, PHP, Java, etc) developers that write pure SQL. There's simply no reason to expose yourself to that level of risk or complication when tools have been created to make it easier, safer, and, generally, smarter. A good ORM will generally write as good or better queries than a human will.

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

@Kenneth Love Would you recommend SQLAlchemy over peewee, as I see it is used more globally?