Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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?