首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在sequel server management studio 2017中使用正确的语法直接运行python代码

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

Stack Overflow用户
提问于 2019-05-13 20:43:41
回答 1查看 410关注 0票数 0

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

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

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

代码语言:javascript
复制
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}")

'

这是我收到的错误

代码语言:javascript
复制
Msg 39004, Level 16, State 20, Line 2
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.
Msg 39019, Level 16, State 2, Line 2
An external script error occurred: 

Error in execution.  Check the output for more information.
Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "C:\PROGRA~1\MICROS~3\MSSQL1~1.MPR\MSSQL\EXTENS~1\MPRYCESQLSEVER01\1D611E8A-CDE1-4F30-9FAC-0BB13871A3DE\sqlindb.py", line 59
    print(f"Houston, we have a problem {e}")
                                          ^
SyntaxError: invalid syntax

SqlSatelliteCall error: Error in execution.  Check the output for more information.
STDOUT message(s) from external script: 
SqlSatelliteCall function failed. Please see the console output for more information.
Traceback (most recent call last):
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MPRYCESQLSEVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\computecontext\RxInSqlServer.py", line 406, in rx_sql_satellite_call
    rx_native_call("SqlSatelliteCall", params)
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MPRYCESQLSEVER\PYTHON_SERVICES\lib\site-packages\revoscalepy\RxSerializable.py", line 291, in rx_native_call
    ret = px_call(functionname, params)
RuntimeError: revoscalepy function failed.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-13 20:51:51

您的环境很可能使用Python 3.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

但是,在Python 3.6中引入了f字符串

所以你要么升级你的Python,要么重写异常处理,例如:

代码语言:javascript
复制
# ORIG: print(f"Houston, we have a problem {e}")
print("Houston, we have a problem {}".format(e))

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

代码语言:javascript
复制
import sys
print(sys.version)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56112634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档