如何在sequel server management studio 2017中使用正确的语法直接运行python代码

2024-10-02 16:24:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个python脚本,它通过“importpyodbc”从sql获取数据。这个脚本提取的数据被解析为一个文本消息网关API,从而向客户发送相应的文本消息。这在python中很好地工作。在

但是,现在我想编写一个sql存储过程,它将在我的业务中每次生成新发票时运行,将phone number+消息的数据发送到同一sql存储过程中的python脚本。在

我现在遇到的问题是用ssms2017编写这个python脚本,并在没有语法错误的情况下执行它。考虑到我使用的是sql2017,我分别启用了python和r。在

execute sp_execute_external_script
@language = N'Python',
@script =  N'

import africastalking

username = "sandbox"
apikey = "bf62be6"
africastalking.initialize(username, apikey)
sms = africastalking.SMS
recipients = ["+254797301255"]
message = ["Test from SQL"]
sender = "MegaLtd"

try:
    response = sms.send(message, recipients, sender)
    print(response)
except Exception as e:
    print(f"Houston, we have a problem {e}")

'

这是我收到的错误

^{pr2}$

Tags: 数据文本脚本消息messageexecutesql过程
1条回答
网友
1楼 · 发布于 2024-10-02 16:24:43

您的环境很可能使用python3.5.2

https://docs.microsoft.com/en-us/sql/advanced-analytics/r/use-sqlbindr-exe-to-upgrade-an-instance-of-sql-server?view=sql-server-2017

但是,在python3.6中引入了f字符串

因此,您要么必须以某种方式升级Python,要么重写异常处理,例如:

# ORIG: print(f"Houston, we have a problem {e}")
print("Houston, we have a problem {}".format(e))

如果您不确定您的环境使用的是哪个Python版本,可以检查一下

^{pr2}$

相关问题 更多 >