> -----Original Message----- > From: sqlalchemy@googlegroups.com > [mailto:[EMAIL PROTECTED] 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/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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---