0

I am just getting my hand dirty with python, hence my question might look silly. Yeah, I have the following code as per of a class. The declared variable in python was to accepted input from the user and then cross checked the content of the variable with an POSTGRES SQL statement. See the code below;

def view_user(self):
    self.contact_modify = input('Please enter name to modify : ')
    contact_modify = self.contact_modify
    try:
        con = self.db_conn()
        print('We are connected to the db now')
    except:
        print('I can not find your database connection here')

    try:
        print(contact_modify) # to check the content of the variable
        con.execute("SELECT * FROM phone WHERE address_name = :'contact_modify'")
        print(contact_modify) # To check the contents of the variable
        while True:
            row = con.fetchone()
            if row == None:
                break
            print(row[1],row[2],row[3])
    except:
        print("Error displaying your data")

I can get the content of the declared variable named 'contact_modify' at all line of code where I print it, but my POSTGRES SQL is not getting the content hence my EXCEPT part of the code is been execute. I am on POSGRES-9.3 and python 3.4 on 64 Windows10 Machine.

3
  • Well you're very cleverly hiding the problem by catching all exceptions and printing a useless nonspecific message instead. Remove that blank except block and let Python print the actual exception. Commented Jan 2, 2016 at 20:50
  • Thanks @DanielRoseman, your observation was almost the solution I had been sought after for more than 3 days now. I am still interesting in catching any error at that stage, can you guide me to the proper thing to do at that stage. Thanks once again. Commented Jan 3, 2016 at 13:35
  • the actual error is as follow; Traceback (most recent call last): File "E:/datafolder/python lesson/testDB.py", line 52, in <module> view.view_user() File "E:/datafolder/python lesson/testDB.py", line 37, in view_user con.execute("SELECT * FROM phone WHERE address_name = :'contact_modify'") psycopg2.ProgrammingError: syntax error at or near ":" LINE 1: SELECT * FROM phone WHERE address_name = :'contact_modify' Commented Jan 3, 2016 at 13:52

2 Answers 2

1

Try This:

  con.execute("""SELECT * FROM phone WHERE address_name ='%s';"""%(contact_modify))

If not work then do as @Daniel Roseman said. remove try and except and see the actual error.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the script work like a charm, after remove the try and except part.
0

You should pass your variable manually, like this:

conn.execute("SELECT foo FROM table where bar = '%s'" % var) 

Also, you can use .format() method.

1 Comment

Thanks, the script work like a charm, after remove the try and except part. I am still interesting in catching the error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.