Python-将变量/对象作为要在函数中使用的参数传递?

Python-将变量/对象作为要在函数中使用的参数传递?,python,function,arguments,Python,Function,Arguments,我还在学习,所以请原谅我,如果我的基本想法,这是如何工作的道路 无论如何,我有两个python程序。程序1用于连接数据库并运行查询。我希望能够从程序2中传递一个变量,以便在程序1的函数中使用。。。如下所示(注意更新查询的*部分): 可以从另一个程序中这样调用它: updateSent(msgId) 我已经到处找过了,但我认为我的措辞不够接近,无法找到我要找的东西。 提前谢谢你 编辑:这是两个模块的完整代码 import pyodbc # declare creds to login to d

我还在学习,所以请原谅我,如果我的基本想法,这是如何工作的道路

无论如何,我有两个python程序。程序1用于连接数据库并运行查询。我希望能够从程序2中传递一个变量,以便在程序1的函数中使用。。。如下所示(注意更新查询的*部分):

可以从另一个程序中这样调用它:

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
差不多

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)
要点是将值MsgIdValue作为函数的参数。您的查询字符串需要此值,在此处添加字符串版本的值


编辑:你似乎有不止一个问题。这只解决了问题的一部分

您需要从另一个模块导入
msgId
。将program1文件导入program2文件并使用函数
updateSent
需要定义为至少接受一个参数(并且
dbUser
等也应作为参数传递,而不是假定为全局变量)。此外,
update\stmt=“update…WHERE msg\u id=%s”
,然后
游标。执行(更新stmt,(msg\u id,)
pyodbc
可以使用与
%s
不同的占位符进行参数化查询;参考文档。你真的有单独的程序还是只有单独的模块?谢谢你-这对于python来说是准确的,我的程序运行没有错误。但是,它不会在数据库中进行更新。这很奇怪,因为我在updateSent()函数中添加了一行print str(msgId),并且它打印了正确的值,所以一切正常,但查询似乎根本没有运行。oops-我没有提交()
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)