[英]Python - pass a variable/object as argument to be used in a function?
I'm still learning, so please forgive me if my fundamental idea of how this works is way off.我仍在学习,所以如果我对它的工作原理的基本想法还很遥远,请原谅我。
Anyways, I have two python programs.无论如何,我有两个 python 程序。 Program 1 functions to connect to a db and run a query.程序 1 用于连接到数据库并运行查询。 I would like to be able to pass a variable from program 2 to be used within a function in program 1... like this (note the * portion of the update query):我希望能够从程序 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)
and be able to call it like this from the other program:并且能够从其他程序中这样调用它:
updateSent(msgId)
I've searched around but I don't think i'm close enough with my wording to find what I'm looking for.我四处搜索,但我认为我的措辞不够接近,无法找到我要找的东西。 Thank you ahead of time.提前谢谢你。
EDIT: here's the full code for both of the modules.编辑:这是两个模块的完整代码。
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)
And the other module:另一个模块:
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
Something like就像是
def updateSent(MsgIdValue):
cnxn = pyodbc.connect('UID='+dbUser+';PWD='+dbPassword+';DSN='+dbHost)
cursor = cnxn.cursor()
update_stmt = "UPDATE WKM_sms_outbound SET status = 'sent' WHERE msg_id = " + str(MsgIdValue)
cursor.execute(update_stmt)
The point is to have the value MsgIdValue as a parameter to the function.关键是将值 MsgIdValue 作为函数的参数。 Your query string needs this value, here by adding a string version of your value.您的查询字符串需要此值,这里通过添加您的值的字符串版本。
Edit: you seem to have more than one question.编辑:您似乎有多个问题。 This only addresses one part of the question这仅解决了问题的一部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.