On Oct 22, 2009, at 6:33 PM, jeff wrote:

>
> I would like to allow user's to save favorite queries in my Python
> app.  Is there a way to find out the SQL statement as a string that
> can be then reused (e.g. Engine.execute(text(savedQueryText) )  )?
> Or is there another solution to this need?


your best bet would be to serialize them using the serializer extension.

http://www.sqlalchemy.org/docs/05/reference/ext/serializer.html?highlight=serializer#module-sqlalchemy.ext.serializer

the examples are ORM centric but you can use select() constructs too -  
the session argument is optional.

otherwise if you just want the raw SQL string, just call str 
(statement).    the string SQL is less wieldy being sent back into  
SQLAlchemy though.



>
> I generate a select object to execute.  The string representations
> don't appear in a format that can be readily reused (or are missing
> the parameter values):
> str(query)
> 'SELECT host_status.host_name, host_status.version,
> host_status.host_id, host_status.sys_uptime, host_status.host_uptime,
> host_status.load_avg, count(%(param_1)s) AS "CMs",
> host_status.db_size, host_status.db_status, host_status.update_time
> \nFROM host_status, cm_status \nWHERE upper(host_status.site) = %
> (upper_1)s AND host_status.host_name = cm_status.host_name GROUP BY
> host_status.host_name, host_status.version, host_status.host_id,
> host_status.sys_uptime, host_status.host_uptime, host_status.load_avg,
> host_status.db_size, host_status.db_status, host_status.update_time
> ORDER BY host_status.host_name ASC'
>
> str(self.execute(query))
> 2009-10-22 16:19:42,642 INFO sqlalchemy.engine.base.Engine.0x...67b0
> SELECT host_status.host_name, host_status.version,
> host_status.host_id, host_status.sys_uptime, host_status.host_uptime,
> host_status.load_avg, count(%(param_1)s) AS "CMs",
> host_status.db_size, host_status.db_status, host_status.update_time
> FROM host_status, cm_status
> WHERE upper(host_status.site) = %(upper_1)s AND host_status.host_name
> = cm_status.host_name GROUP BY host_status.host_name,
> host_status.version, host_status.host_id, host_status.sys_uptime,
> host_status.host_uptime, host_status.load_avg, host_status.db_size,
> host_status.db_status, host_status.update_time ORDER BY
> host_status.host_name ASC
> 2009-10-22 16:19:42,642 INFO sqlalchemy.engine.base.Engine.0x...67b0
> {'param_1': 'cm_status.host_name', 'upper_1': 'LCO'}
> '<sqlalchemy.engine.base.ResultProxy object at 0x01D33F90>'
>
> Thanks!
>
> >


--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to