in order to debug my code, i wish to print my query sql.
it's in the fashion of
query = table.query().filter(table.code='XL').filter(table.name.like('%'+q+'%')
with unicode parameters.
by just printing query, i get the select with ? parameters, but not
the additional parameters list, that contains ['XL', %q-value%]. since
it doesn't presently work ok, i'd like to print the list as well.
thanks in advance,
alex
print qry.compile()
Glauco
--
+------------------------------------------------------------+
Glauco Uri
glauco(at)sferacarta.com
Sfera Carta SoftwareŽ info(at)sferacarta.com
Via Bazzanese,69 Casalecchio di Reno(BO) - Tel. 051591054
+------------------------------------------------------------+
thanks for your answer.
but it unfortunately doesn't work for me.
could it be because i'm using elixir?
best regards,
alex
On Wed, Oct 15, 2008 at 12:10, Glauco <gla...@sferacarta.com> wrote:
>
>
>> by just printing query, i get the select with ? parameters, but not
>> the additional parameters list, that contains ['XL', %q-value%]. since
>> it doesn't presently work ok, i'd like to print the list as well.
>>
>
>
> print qry.compile()
>
>
> Glauco
>
> --
> +------------------------------------------------------------+
> Glauco Uri
> glauco(at)sferacarta.com
>
> Sfera Carta Software(R) info(at)sferacarta.com
This question comes up a lot. For example, see
http://groups.google.com/group/sqlalchemy/browse_thread/thread/a0602ede8
18f55c7
Firstly, if you use echo=True in your call to create_engine, all SQL
will be printed to stdout. The parameters will be displayed as a list
AFTER the SQL is printed.
Eg. (from http://www.sqlalchemy.org/docs/05/ormtutorial.html)
BEGIN
INSERT INTO users (name, fullname, password) VALUES (?, ?, ?)
['ed', 'Ed Jones', 'edspassword']
SELECT users.id AS users_id, users.name AS users_name, users.fullname AS
users_fullname, users.password AS users_password
FROM users
WHERE users.name = ?
LIMIT 1 OFFSET 0
['ed']
You can control the logging more finely using the logging module - see
http://www.sqlalchemy.org/docs/05/dbengine.html#dbengine_logging for
more details.
The problem is that SQLAlchemy doesn't ever replace those '?' characters
with the actual parameter values. Those strings are passed directly to
the DBAPI driver, along with the list of parameter values. It is then up
to the DBAPI driver how it passes the query to the database. (This is
why SQLAlchemy is fairly safe from SQL Injection attacks).
Hope that helps,
Simon
class T_mark( unittest.TestCase):
...
def setUp( self):
self.m = MetaData()
#hack for better visibility
def bp( self,bindparam):
if bindparam.value is not None:
return 'const('+repr(bindparam.value)+')'
k = bindparam.key
if k.startswith( Converter._pfx): #my own bindparams
k = k[ len( Converter._pfx):]
return 'BindParam('+k+')'
self.old_bp = DefaultCompiler._truncate_bindparam
DefaultCompiler._truncate_bindparam = bp
def tearDown( self):
DefaultCompiler._truncate_bindparam = self.old_bp
...
str(expression) then does things like
:const(True) AND :BindParam(oid) = movies.id
tags.tabl = :const('movies') AND tags.oid = :BindParam(oid)
there's some more stuff going on there around compatibility with SA
0.3--0.5, but that's core.
ciao
svil
On Wednesday 15 October 2008 13:33:46 King Simon-NFHD78 wrote:
> > -----Original Message-----
> > From: sqlal...@googlegroups.com
> > [mailto:sqlal...@googlegroups.com] On Behalf Of alex bodnaru
> > Sent: 15 October 2008 11:00
> > To: SQLAlchemy
> > Subject: [sqlalchemy] how to print a constructed query with
> > it's parameters?
> >
> >
> > hello friends,
> >
> > in order to debug my code, i wish to print my query sql.
> >
> > it's in the fashion of
> > query =
> > table.query().filter(table.code='XL').filter(table.name.like('
> > %'+q+'%')
> > with unicode parameters.
> >
> > by just printing query, i get the select with ? parameters, but
> > not the additional parameters list, that contains ['XL',
> > %q-value%]. since it doesn't presently work ok, i'd like to print
> > the list as well.
> >
> > thanks in advance,
> > alex
>
> This question comes up a lot. For example, see
> http://groups.google.com/group/sqlalchemy/browse_thread/thread/a060
>2ede8 18f55c7
i have a lot to learn from both approaches, but i have sadly appeared too lazy.
there will be no problem to imagine what the sql will be, only by
looking at the
template statement (with ?'s) and at the list of parameters.
since the template is available to print (probably by __str__), i'd
onlu ask where
the bindparams list is. eventual quotes and escapes may be imagined by
the types of
the columns.
thanks in advance,
alex
thank you very much.
it will help me and many others in printf style debugging.
best regards,
alex