Skip to content

Instantly share code, notes, and snippets.

@hhatto
Created November 28, 2012 17:13
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhatto/4162611 to your computer and use it in GitHub Desktop.
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))
@paolodina
Copy link

In recent version of peewee:

compiler = database.compiler()

@FrankMN
Copy link

FrankMN commented Sep 23, 2016

In the most recent version, no compiler param is needed. The result is a tuple though. Any way to get a SQL query with parameters filled in it?

@ajbisberg
Copy link

Yeah I ran in to this same problem, is there any way to print the sql with the parameters filled in?

@jontonsoup
Copy link

bump

@moacirmoda
Copy link

Me too

@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