Beginner: printing out queries

616 views
Skip to first unread message

bukzor

unread,
Jun 24, 2008, 9:27:54 PM6/24/08
to sqlalchemy
Is there a way to print out the query as it would execute on the
server? I'd like to copy/paste it into the server to get the 'explain'
output, and the '%s' variables are very unhelpful here.

I'd also like to turn off the 'alias and backtick-escape every column'
default behavior if I can.

Thanks!
--Buck

Hopefully I'll be able to *answer* questions someday soon...

Michael Bayer

unread,
Jun 25, 2008, 10:25:16 AM6/25/08
to sqlal...@googlegroups.com

On Jun 24, 2008, at 9:27 PM, bukzor wrote:

>
> Is there a way to print out the query as it would execute on the
> server? I'd like to copy/paste it into the server to get the 'explain'
> output, and the '%s' variables are very unhelpful here.

the string output of str(statement) is what's actually sent to the
client lib. In some cases, that is the actual string that goes to
the server, such as cx_oracle, which receives the bind parameters
separately within the network conversation. The fact that MySQLDB and
psycopg2 do an in-client "substitution" of the string before passing
on is an implementation artifact of those libraries.

Feel free to construct the string yourself (this is specific to
MySQL's bind param style):

stmt = str(statement)
compiled = statement.compile()
params = compiled.params
stmt = stmt % [params[k] for k in compiled.positiontup]


> I'd also like to turn off the 'alias and backtick-escape every column'
> default behavior if I can.

we don't "backtick every column". We quote case sensitive idenfitier
names, if that's what you mean, where "case sensitive" is any
identifier that is spelled out in MixedCase - this is required for the
column to be properly recognized by the database. Use all lower case
letters to indicate a "case insensitive" identifier.

bukzor

unread,
Jun 25, 2008, 2:14:38 PM6/25/08
to sqlalchemy
Thanks.

Trying to do this in 0.5, it seems someone deleted the Query.compile()
method without updating the rest of the code:
Traceback (most recent call last):
File "./test1.py", line 139, in ?
try: exit(main(*argv))
File "./test1.py", line 121, in main
print_query(q)
File "./test1.py", line 20, in print_query
print str(q)
File "/python-2.4.1/lib/python2.4/site-packages/
SQLAlchemy-0.5.0beta1-py2.4.egg/sqlalchemy/orm/query.py", line 1448,
in __str__
return str(self.compile())
AttributeError: 'Query' object has no attribute 'compile'

Reverting to 0.4, there are other problems. Statement.params is a
function, so I added some ()'s, but it just returns the same query
again. Statement.positiontup doesn't exist, and the string stmt
doesn't have formatting to make use of python's % operator.

After about an hour of looking, I can't figure out how to get my
scalars out of the query object.

Michael Bayer

unread,
Jun 25, 2008, 2:23:59 PM6/25/08
to sqlal...@googlegroups.com

On Jun 25, 2008, at 2:14 PM, bukzor wrote:

>
> Thanks.
>
> Trying to do this in 0.5, it seems someone deleted the Query.compile()
> method without updating the rest of the code:
> Traceback (most recent call last):
> File "./test1.py", line 139, in ?
> try: exit(main(*argv))
> File "./test1.py", line 121, in main
> print_query(q)
> File "./test1.py", line 20, in print_query
> print str(q)
> File "/python-2.4.1/lib/python2.4/site-packages/
> SQLAlchemy-0.5.0beta1-py2.4.egg/sqlalchemy/orm/query.py", line 1448,
> in __str__
> return str(self.compile())
> AttributeError: 'Query' object has no attribute 'compile'

its been fixed in trunk. Keep an eye on http://www.sqlalchemy.org/trac/browser/sqlalchemy/trunk/CHANGES
. beta2 is out very soon (like, this week).

> Reverting to 0.4, there are other problems. Statement.params is a
> function, so I added some ()'s, but it just returns the same query
> again. Statement.positiontup doesn't exist, and the string stmt
> doesn't have formatting to make use of python's % operator.

my code example used the 0.5 APIs, but in all cases you have to get
the "compiled" object first. This is described in the tutorials for
both 0.4 and 0.5, such as at http://www.sqlalchemy.org/docs/05/sqlexpression.html#sql_insert
.


bukzor

unread,
Jun 27, 2008, 1:49:17 PM6/27/08
to sqlalchemy
Why is this so hard?? In SQLObject, this "problem" consists entirely
of:

print queryobj

Bye all,
--Buck

On Jun 25, 11:23 am, Michael Bayer <mike...@zzzcomputing.com> wrote:
> On Jun 25, 2008, at 2:14 PM, bukzor wrote:
>
>
>
>
>
> > Thanks.
>
> > Trying to do this in 0.5, it seems someone deleted the Query.compile()
> > method without updating the rest of the code:
> > Traceback (most recent call last):
> > File "./test1.py", line 139, in ?
> > try: exit(main(*argv))
> > File "./test1.py", line 121, in main
> > print_query(q)
> > File "./test1.py", line 20, in print_query
> > print str(q)
> > File "/python-2.4.1/lib/python2.4/site-packages/
> > SQLAlchemy-0.5.0beta1-py2.4.egg/sqlalchemy/orm/query.py", line 1448,
> > in __str__
> > return str(self.compile())
> > AttributeError: 'Query' object has no attribute 'compile'
>
> its been fixed in trunk. Keep an eye onhttp://www.sqlalchemy.org/trac/browser/sqlalchemy/trunk/CHANGES

Michael Bayer

unread,
Jun 27, 2008, 1:56:38 PM6/27/08
to sqlal...@googlegroups.com

On Jun 27, 2008, at 1:49 PM, bukzor wrote:

>
> Why is this so hard?? In SQLObject, this "problem" consists entirely
> of:
>
> print queryobj

in SQLAlchemy it is also:

print queryobj

how is that harder ?


Reply all
Reply to author
Forward
0 new messages