0

I have a "batch" table on my postgresql DB, and I would like to change a single value within column "id", replacing 70 with 15:

from sqlalchemy import create_engine

# Connection with DB
engine = create_engine('postgresql://xxx')

engine.connect()

# Update the value
...
Alessandro Ceccarelli
  • 1,775
  • 5
  • 21
  • 41
  • you can use the SQL statement: `UPDATE batch SET id=15 WHERE id=70`. How to connect to PostgreSQL from sqlalchemy is answered here: https://stackoverflow.com/questions/9353822/connecting-postgresql-with-sqlalchemy – Luuk Sep 19 '21 at 09:42
  • Could you please complete the above code to fully answer? Thank you – Alessandro Ceccarelli Sep 19 '21 at 09:47
  • Could you please take a try and do that yourself? (SO is not a [code writing service](https://meta.stackexchange.com/questions/69798/is-stack-overflow-a-code-writing-service)) – Luuk Sep 19 '21 at 09:50
  • I know, could you please check the edit? Thank you – Alessandro Ceccarelli Sep 19 '21 at 09:54
  • Which edit ? and please mark your own answer as the 'accepted answer'. (If that solved your question. If not, then that answer should not have been posted as answer, but as an [edit] to your question. – Luuk Sep 19 '21 at 12:11

1 Answers1

0
# Connection with DB
engine = create_engine('postgresql://xxx')
engine.connect()

# Update the value

sql = f"""
    UPDATE batch
    SET id=15 
    WHERE id=70
"""

with engine.begin() as conn:     # TRANSACTION
    conn.execute(sql)
Alessandro Ceccarelli
  • 1,775
  • 5
  • 21
  • 41