SELECT, UPDATE, and DELETE

This commit is contained in:
slashtechno 2024-06-04 10:56:15 -05:00
parent 63fa8e39b8
commit 0eaa8296a0
Signed by: slashtechno
GPG Key ID: 8EC1D9D9286C2B17
1 changed files with 12 additions and 0 deletions

View File

@ -1,5 +1,6 @@
# https://www.freecodecamp.org/news/postgresql-in-python/
# https://www.datacamp.com/tutorial/tutorial-postgresql-python
# docker compose down && docker compose up -d && hello-postgres
import psycopg2
import dotenv
import os
@ -32,7 +33,18 @@ def main():
cursor.execute("INSERT INTO datacamp_courses(course_name, course_instructor, topic) VALUES('Hypothesis Testing in Python','James Chapman','Python')");
print("Committing the table to the database...")
conn.commit()
print("Fetching all the available rows...")
cursor.execute('SELECT * FROM datacamp_courses;')
rows = cursor.fetchall()
for row in rows:
print(row)
print("Closing the connection and the cursor...")
print("Changing topic \"Julia\" to \"SQL\"...")
cursor.execute("UPDATE datacamp_courses SET topic = 'SQL' WHERE course_name = 'Introduction to SQL';")
conn.commit()
print("Deleting \"Introduction to Statistics in R\"...")
cursor.execute("DELETE from datacamp_courses WHERE course_name = 'Introduction to Statistics in R';")
conn.commit()
cursor.close()
conn.close()
# cursor.execute("SELECT * FROM DB_table WHERE id = 1")