Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import psycopg2

def connect():
""" Connect to the PostgreSQL database server """
conn = psycopg2.connect(
host="localhost",
database="pagila",
user="postgres",
password="postgres")
try:

# connect to the PostgreSQL server
print('Connecting to the PostgreSQL database...')

# create a cursor
cur = conn.cursor()

# execute a statement
cur.execute('select * from "actor"')

# display the PostgreSQL database server version
db_version = cur.fetchmany(5)
# db_version = cur.fetchall()
# db_version = cur.fetchone()
for i in db_version:
print(i)


# close the communication with the PostgreSQL
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')

if __name__ == '__main__':
connect()
74 changes: 74 additions & 0 deletions create_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import psycopg2

def create_tables():
""" create tables in the PostgreSQL database"""
commands = (
"""

CREATE TABLE IF NOT EXISTS public.books
(
book_id serial NOT NULL ,
title character varying(100) COLLATE pg_catalog."default" NOT NULL,
author character varying(100) COLLATE pg_catalog."default",
genre character varying(100) COLLATE pg_catalog."default",
pages integer,
added_date date DEFAULT CURRENT_DATE,
quantity integer NOT NULL,
CONSTRAINT books_pkey PRIMARY KEY (book_id),
CONSTRAINT uk_books UNIQUE (title, author)
)
""",
""" CREATE TABLE IF NOT EXISTS public.user_action
(
action_id serial NOT NULL,
user_id integer NOT NULL,
book_id integer NOT NULL,
borrow boolean DEFAULT false,
reading boolean DEFAULT false,
read boolean DEFAULT false,
fav boolean DEFAULT false,
will_read boolean DEFAULT false,
CONSTRAINT pk_action_id PRIMARY KEY (action_id)
)
""",
"""CREATE TABLE IF NOT EXISTS public.users
(
user_id serial NOT NULL,
user_name character varying(100) NOT NULL,
CONSTRAINT pk_user_id PRIMARY KEY (user_id),
CONSTRAINT uk_user_name UNIQUE (user_name)
)
""",
"""ALTER TABLE IF EXISTS public.user_action
ADD CONSTRAINT fk_books_actions FOREIGN KEY (book_id)
REFERENCES public.books (book_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION;""",
"""ALTER TABLE IF EXISTS public.user_action
ADD FOREIGN KEY (user_id)
REFERENCES public.users (user_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID;

"""
)
conn = psycopg2.connect(
host="localhost",
database="library",
user="postgres",
password="postgres")
try:
cur = conn.cursor()
for command in commands:
cur.execute(command)
cur.close()
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()

if __name__ == '__main__':
create_tables()
48 changes: 48 additions & 0 deletions exercise5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import psycopg2
# from config import config


def create_tables():
""" create tables in the PostgreSQL database"""
commands = (
"""

CREATE TABLE IF NOT EXISTS public.students (
student_id SERIAL PRIMARY KEY,
student_name VARCHAR(255) NOT NULL
)
""",
""" CREATE TABLE IF NOT EXISTS public.teachers (
teacher_id SERIAL PRIMARY KEY,
teacher_name VARCHAR(255) NOT NULL
)
""")
conn = psycopg2.connect(
host="localhost",
database="PyCoders",
user="postgres",
password="postgres")
try:
cur = conn.cursor()
for command in commands:
cur.execute(command)

student_list=['sefa','saffet','ilter']
for i in student_list:
postgres_insert_query = f""" INSERT INTO students (student_name) VALUES ('{i}')"""
cur.execute(postgres_insert_query)

teacher_list=['irfan','semih','irem']
for i in teacher_list:
postgres_insert_query2 = f""" INSERT INTO teachers (teacher_name) VALUES ('{i}')"""
cur.execute(postgres_insert_query2)
cur.close()
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()

if __name__ == '__main__':
create_tables()
Loading