SELECT, UPDATE, and DELETE
This commit is contained in:
parent
63fa8e39b8
commit
0eaa8296a0
|
@ -1,5 +1,6 @@
|
||||||
# https://www.freecodecamp.org/news/postgresql-in-python/
|
# https://www.freecodecamp.org/news/postgresql-in-python/
|
||||||
# https://www.datacamp.com/tutorial/tutorial-postgresql-python
|
# https://www.datacamp.com/tutorial/tutorial-postgresql-python
|
||||||
|
# docker compose down && docker compose up -d && hello-postgres
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import dotenv
|
import dotenv
|
||||||
import os
|
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')");
|
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...")
|
print("Committing the table to the database...")
|
||||||
conn.commit()
|
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("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()
|
cursor.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
# cursor.execute("SELECT * FROM DB_table WHERE id = 1")
|
# cursor.execute("SELECT * FROM DB_table WHERE id = 1")
|
||||||
|
|
Loading…
Reference in New Issue