我还在学习,所以请原谅我,如果我的基本想法,这是如何工作的道路
无论如何,我有两个python程序。程序1用于连接数据库并运行查询。我希望能够从程序2中传递一个变量,以便在程序1的函数中使用。。。如下所示(注意更新查询的*部分):
def updateSent():
cnxn = pyodbc.connect('UID='+dbUser+';PWD='+dbPassword+';DSN='+dbHost)
cursor = cnxn.cursor()
update_stmt = "UPDATE WKM_sms_outbound SET status = 'sent' WHERE msg_id = ****VALUE FROM msgId****"
cursor.execute(update_stmt)
可以从另一个程序中这样调用它:
updateSent(msgId)
我已经到处找过了,但我认为我的措辞不够接近,无法找到我要找的东西。 提前谢谢你
编辑:这是两个模块的完整代码
import pyodbc
# declare creds to login to db
dbUser="dba"
dbPassword="sql"
dbHost="Needles"
dbPort="2638"
dbConnection=None
# function to fetch/return latest sms from needles db
def pullData():
# connect
cnxn = pyodbc.connect('UID='+dbUser+';PWD='+dbPassword+';DSN='+dbHost)
cursor = cnxn.cursor()
# run query & store to dictionary
outbound = cursor.execute("SELECT * FROM WKM_SMS_outbound ORDER BY id DESC")
results = cursor.fetchone()
collection = {}
for index, outbound in enumerate(results):
key_name = "col{0}".format(index)
collection[key_name] = outbound
# store desired keys/values to vars
msg = collection['col1']
destPhone = collection['col2']
msgId = collection['col3']
caseNum = collection['col4']
sender = collection['col10']
# close connection
cnxn.close()
# return desired fields for sending
return (msg, destPhone, msgId, caseNum, sender)
def updateSent():
cnxn = pyodbc.connect('UID='+dbUser+';PWD='+dbPassword+';DSN='+dbHost)
cursor = cnxn.cursor()
update_stmt = "UPDATE WKM_sms_outbound SET status = 'sent' WHERE msg_id = *********"
cursor.execute(update_stmt)
另一个模块:
import requests
from otherModule import pullData
from otherModule import updateSent
from datetime import datetime
from Logger import Logger
# call function from other module to pull sms
msg, destPhone, msgId, caseNum, sender = pullData()
# declare vars
msg=msg
destPhone=destPhone
sender=sender
msgId=str(msgId)
# headers and parameters required for api
headers = {
'x-api-token': '1130FxmIcc****oCZZPCpq8ODQo',
'x-api-key': 'pdwz0naW5hyC****nOf26BZFS28',
'content-type': 'multipart/form-data; boundary=---'
}
formData = {
'locationId': '2045',
'messageBody': msg,
'contactPhone': destPhone
}
# POST & log the sms request
Logger.writeAndPrintLine('[REQ: ' + msgId + '] ' + sender + ' to ' + destPhone, 1)
updateSent(msgId)
r=requests.post('https://api.***.com/v1/conversations/messages', headers=headers, params=formData).text
差不多
要点是将值MsgIdValue作为函数的参数。您的查询字符串需要此值,在此处添加字符串版本的值
编辑:你似乎有不止一个问题。这只解决了问题的一部分
相关问题 更多 >
编程相关推荐