Retrieving ultimate sql query sentence (with the values in place of any '?')

眉间皱痕 提交于 2019-12-25 04:26:42

问题


Since it may be efficient to paste a flawed sql query directly into a database administration tool such as phpmyadmin in order to work on it until it returns the expected result, Is there any way to retrieve the ultimate sql sentence Sqlalchemy Core supposedly passes to the MySql database, in a ready-to-execute shape ?


回答1:


This typically means that you want the bound parameters to be rendered inline. There is limited support for this automatically (as of SQLA 0.9 this will work):

from sqlalchemy.sql import table, column, select

t = table('x', column('a'), column('b'))

stmt = select([t.c.a, t.c.b]).where(t.c.a > 5).where(t.c.b == 10)

print(stmt.compile(compile_kwargs={"literal_binds": True}))

also you'd probably want the query to be MySQL specific, so if you already have an engine lying around you can pass that in too:

from sqlalchemy import create_engine
engine = create_engine("mysql://")

print(stmt.compile(engine, compile_kwargs={"literal_binds": True}))

and it prints:

SELECT x.a, x.b 
FROM x 
WHERE x.a > 5 AND x.b = 10

now, if you have more elaborate values in the parameters, like dates, SQLAlchemy might throw an error, it only has "literal binds" renderers for a very limited number of types. An approach that bypasses that system instead and gives you a pretty direct shot at turning those parameters into strings is then do to a "search and replace" on the statement object, replacing the bound parameters with literal strings:

from sqlalchemy.sql import visitors, literal_column
from sqlalchemy.sql.expression import BindParameter

def _replace(arg):
    if isinstance(arg, BindParameter):
        return literal_column(
                repr(arg.effective_value)  # <- do any fancier conversion here
            )
stmt = visitors.replacement_traverse(stmt, {}, _replace)

once you do that you can just print it:

print(stmt)

or the MySQL version:

print(stmt.compile(engine))


来源:https://stackoverflow.com/questions/21735943/retrieving-ultimate-sql-query-sentence-with-the-values-in-place-of-any

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!