Skip to content

Instantly share code, notes, and snippets.

@hhatto
Created November 28, 2012 17:13
Show Gist options
  • Select an option

  • Save hhatto/4162611 to your computer and use it in GitHub Desktop.

Select an option

Save hhatto/4162611 to your computer and use it in GitHub Desktop.
print raw sql for peewee
from peewee import *
from models import News, database
compiler = database.get_compiler()
print(News.select().where(News.url == 'test').sql(compiler))
@isimluk
Copy link

isimluk commented Jun 26, 2017

👍

Copy link

ghost commented Oct 14, 2017

peewee2.10.2 can use SelectQuery.sql() to print the sql

@isthisthat
Copy link

This works:

query = News.select().where(News.url == 'test')
sql, param = query.sql()
print sql.replace("?","{}").format(*param)

@tijptjik
Copy link

tijptjik commented Feb 9, 2020

As per peewee 3.13 you can print SQL queries by first getting the cursor, then calling the mogrify() method for your query.

query = <YOUR PEEWEE QUERY>

cur = database.cursor()
print(cur.mogrify(*query.sql()))

Where database is the Peewee Database object representing the connection to your database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment