October 15th, 2013, 04:58 AM
Variables passing in the run the run time.
I need to pass the variable to a python program consisting of special character in it.
Not sure if it is silly thing or not. i am new to the python programing.
Here a small script used to connect the local postgres db and run a query.
>/usr/bin/python emp.py emp-1345
import psycopg2 as pdb
conn_string = "host='localhost' dbname='emp' user='emp' password='emp'"
conn = pdb.connect (conn_string)
cursor = conn.cursor ()
empid = sys.argv
print "Arg1", empid
cursor.execute ("SELECT sal FROM emp.empfieldvalue WHERE eid = (SELECT id FROM emp.empinfo WHERE id = 'empid') AND dept_id= '10055'")
records = cursor.fetchall()
Issue: i am able get the empid in the print statement. but coming to the query it's returning nothing(i am able to get the info by running manually on the db).
October 15th, 2013, 09:46 AM
If you want to pass a variable, no quotes, but I am not that familar with PostGreSQL. In SQLite you use a dictionary to provide a variable to a select statement, so you should try to find a tutorial on the web for PostGreSQL and learn from it.
cursor.execute ("SELECT sal FROM emp.empfieldvalue WHERE eid = (SELECT id FROM emp.empinfo WHERE id =empid) AND dept_id= '10055'")
Also, the type of container records returns depends on your version of python, so you may have to convert records to a list.
Last edited by dwblas; October 15th, 2013 at 09:55 AM.