Learn how to effectively utilize Python with the powerfulPostgreSQL database. PostgreSQL, a robust relational database management system (RDBMS), boasts of features like foreign keys, joins, views, triggers, stored procedures, and much more. By the end of this guide, you’ll be familiar with the essentials of integrating PostgreSQL with Python.
To commence, ensure you have both the PostgreSQL DBMS and thepsycopg2 Python module. For those on an Ubuntu system, you can swiftly install the PostgreSQL using the following:
try: con = psycopg2.connect("host='localhost' dbname='testdb' user='pythonspot' password='password'") cur = con.cursor() cur.execute("UPDATE Products SET Price=%s WHERE Id=%s", (10,4)) con.commit() except psycopg2.DatabaseErroras e: if con: con.rollback() print(f'Error:{e}') sys.exit(1) finally: if con: con.close()
try: con = psycopg2.connect("host='localhost' dbname='testdb' user='pythonspot' password='password'") cur = con.cursor() cur.execute("DELETE FROM Products WHERE Id=" +str(4)) con.commit() except psycopg2.DatabaseErroras e: if con: con.rollback() print(f'Error:{e}') sys.exit(1) finally: if con: con.close()
Navigate through more Python database interactions:Back |Next