打印MySQLdb运行时的实际查询? [英] Print the actual query MySQLdb runs?

查看:70
本文介绍了打印MySQLdb运行时的实际查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在执行查询时调试查询的方法,我想知道是否有一种方法可以使MySQLdb在完成参数插入及所有操作之后打印出它所运行的实际查询?从文档中看,似乎应该有一个Cursor.info()调用,该调用将提供有关上次查询运行的信息,但是在我的版本(1.2.2)中不存在.

I'm looking for a way to debug queries as they are executed and I was wondering if there is a way to have MySQLdb print out the actual query that it runs, after it has finished inserting the parameters and all that? From the documentation, it seems as if there is supposed to be a Cursor.info() call that will give information about the last query run, but this does not exist on my version (1.2.2).

这似乎是一个显而易见的问题,但是对于我所有的搜索,我一直找不到答案.预先感谢.

This seems like an obvious question, but for all my searching I haven't been able to find the answer. Thanks in advance.

推荐答案

我们在游标对象上找到了一个名为cursor._last_executed的属性,该属性保存了即使发生异常也要运行的最后一个查询字符串.与一直使用概要分析或MySQL查询日志记录相比,这对我们在生产中更容易和更好,因为这两种方法都会影响性能,并涉及更多代码或更多相关的独立日志文件,等等.

We found an attribute on the cursor object called cursor._last_executed that holds the last query string to run even when an exception occurs. This was easier and better for us in production than using profiling all the time or MySQL query logging as both of those have a performance impact and involve more code or more correlating separate log files, etc.

讨厌回答我自己的问题,但这对我们来说更好.

Hate to answer my own question but this is working better for us.

这篇关于打印MySQLdb运行时的实际查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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