1

I have written a script to update my employee table in psycopg2 but the update is not happening the currr.execute is returning a None object.

my code

            connm = psycopg2.connect(database="database", user = "user", password = "pass", host = "localhost", port = "5432")
            connm.autocommit = True
            sql = """ UPDATE employee SET name = %s WHERE phone_number = %s"""
            curr = connm.cursor()
            username = "Mike"
            query = "+0123456789"
            abc = curr.execute(sql, (username, query))
            print abc
            connm.commit()
            curr.close()

This abc object is returning None.

Kindly help me! Thanks in advance!

2
  • What do you expect to print? I think you have more to change if you want a result than just adding fetch. Maybe a RETURNING in the update? Commented Nov 30, 2017 at 9:27
  • @ChrisTravers I want to check what is returning in my pgadmin Im constantly checking if the update has happened or not. Commented Nov 30, 2017 at 9:30

1 Answer 1

1

Quoting "Psycopg – PostgreSQL database adapter for Python - execute command":

The method returns None. If a query was executed, the returned values can be retrieved using fetch*() methods

Then, None looks fine. Congrats :)

Edited about no effect:

Can you test your query on database?

my_raw_query=curr.mogrify(sql, (username, query))
print my_raw_query

Check if query is ok and copy paste query on database to check it again.

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

4 Comments

The query manually executed in the pg admin worked the query is . " UPDATE employee SET name = 'D****r' WHERE phone_number = '+ 9*****6 (M) '; " But through psycopg2 its not happening!
Sure are you working on same database? Please, double check connection string.
Also, did you try to catch exceptions? initd.org/psycopg/docs/module.html#exceptions
There are no exceptions or errors simply the update is not happening I have tried eveything!

Your Answer

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