Jan-14-2020, 10:06 PM
Hi everybody,
I'm working on a program that involves querying a database with pyodbc and working with the results. below you will see my program, and the function I'm working with here. The end goal is to be able to work with the returned variables to be able to import/pass them along to another python program.
As you can see, the function works to connect to a database, run a query, and stores the query results in a dictionary. After that, I assign the specific dictionary keys to variables and "return" them at the end of the function. As noted above, I need to be able to import these variables (msg, destPhone, caseNum, timeStamp, sender) from another program... however when I try to print one of them (to test), I don't get any result.
Forgive me for the noobish question, but I'm new to python.. and new around here.
Thank you for your time!
I'm working on a program that involves querying a database with pyodbc and working with the results. below you will see my program, and the function I'm working with here. The end goal is to be able to work with the returned variables to be able to import/pass them along to another python program.
As you can see, the function works to connect to a database, run a query, and stores the query results in a dictionary. After that, I assign the specific dictionary keys to variables and "return" them at the end of the function. As noted above, I need to be able to import these variables (msg, destPhone, caseNum, timeStamp, sender) from another program... however when I try to print one of them (to test), I don't get any result.
Forgive me for the noobish question, but I'm new to python.. and new around here.
Thank you for your time!
def pullData(): cnxn = pyodbc.connect('UID='+dbUser+';PWD='+dbPassword+';DSN='+dbHost) cursor = cnxn.cursor() 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 msg = collection['col1'] destPhone = collection['col2'] caseNum = collection['col4'] timeStamp = collection['col8'] sender = collection['col10'] cnxn.close() return (msg, destPhone, caseNum, timeStamp, sender) pullData() print(msg)