psycopg-获取格式化的sql而不是执行 [英] psycopg - Get formatted sql instead of executing

查看:88
本文介绍了psycopg-获取格式化的sql而不是执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段Python代码,可通过psycopg与PostgreSQL数据库进行交互。

I have a piece of Python code, that interacts with a PostgreSQL database via psycopg.

所有文献都警告不要自己进行sql格式化,并建议让驱动程序来做。例如:

All literature warns against doing sql formatting by oneself, and recommends letting the driver do it. E.g.:

cur.execute('select name, age from people where name = %s;', ('ann',) )

驱动程序然后格式化sql字符串。假设我不想执行任何操作,但只想要完全格式化的sql字符串。在psycopg模块中是否有获取此格式的sql的功能?

The driver then formats the sql string. Let's say I don't want to execute anything, but I just want the fully formatted sql string. Is there any functionality for getting this formatted sql in the psycopg module?

推荐答案

您希望使用函数curs.mogrify():

you wold use function curs.mogrify():

SQLstring = curs.mogrify('select name, age from people where name = %s;', ('ann',) )

这篇关于psycopg-获取格式化的sql而不是执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆