#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Smile 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

    #!/usr/bin/python
    import psycopg2 as pdb
    import sys
    import pprint

    conn_string = "host='localhost' dbname='emp' user='emp' password='emp'"
    conn = pdb.connect (conn_string)
    cursor = conn.cursor ()
    empid = sys.argv[1]
    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()
    pprint.pprint(records)

    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).
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    509
    Rep Power
    33
    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.

IMN logo majestic logo threadwatch logo seochat tools logo