如何使用pymssql获取sql打印消息 [英] How to get the sql print message using pymssql

查看:140
本文介绍了如何使用pymssql获取sql打印消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一些查询,从它们的执行中打印运行时统计信息.

I'm running some queries, that print runtime stats from their execution.

已经完成了print('message')在 sql 脚本中使用.

It's done through print('message') used within the sql script.

我希望在通过 pymssql 调用过程/脚本时看到这些消息.

I would want to see these messages while calling the procedures/scripts through pymssql.

conn = pymssql.connect(server, user, password, "tempdb")
cursor = conn.cursor()
cursor.execute("print('message')")
conn.commit()

上面的脚本没有返回任何内容,而且我找不到任何关于如何让打印显示在控制台输出中的提示.

Above script doesn't return anything, and I can't find any tips on how to get that print to show up in the console output.

推荐答案

找到了一个解决方案,让我仍然使用 pymssql 并获取打印消息.pymssql.Connection 实际上在内部使用了 _mssql.MSSQLConnection.这意味着您可以通过访问该内部文件来使用this 示例对象.

Found a solution that let's me still use pymssql and get the print messages. pymssql.Connection actually uses _mssql.MSSQLConnection internally. This means that you can use this example by accessing that internal object.

connection = pymssql.connect(server='server_address', database='db_name')
connection._conn.set_msghandler(my_msg_handler)  # Install our custom handler

其中 my_msg_handler 与 pmssql wiki 中的对象类型相同.访问内部对象并不理想,但如果您不想使用不同的库并且需要获取 SQL 打印,这是我发现的唯一方法.

where the my_msg_handler is the same type of object as in pmssql wiki. Accessing internal objects is not ideal, but it's the only way I've found if you don't want to use a different library and need to get the SQL prints.

这篇关于如何使用pymssql获取sql打印消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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