import psycopg2

dbname='llm'
user='postgres'
host='localhost'
password='123'
id=1

try:
    conn = psycopg2.connect(f"dbname={dbname} user={user} host={host} password={password} ")
except:
    print("I am unable to connect to the database")


# we use a context manager to scope the cursor session
with conn.cursor() as curs:
    try:
        # simple single row system query
        curs.execute("""
           CREATE TABLE IF NOT EXISTS cinemas (
                    id serial,
                    name text,
                    describe text
            );
            """)
        print("OK")
        conn.commit()
    # a more robust way of handling errors
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    try:
        curs.execute("""
            INSERT
            INTO
            cinemas("name", describe)
            VALUES('Иллюзия обмана', 'Про фокусы !');
        """)
        conn.commit()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)

    try:
        curs.execute(f"""
            DELETE
            FROM
            cinemas 
            WHERE ID={id}
        """)
        conn.commit()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)

    try:
        curs.execute(f"""
            UPDATE cinemas SET name='Иллюзия любви!', describe='No name'
            WHERE ID=2
        """)
        conn.commit()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)


    try:
        curs.execute(f"""
            SELECT id, name, describe
             FROM cinemas
             
        """)

        _rows = curs.fetchall()
        for row in _rows:
            id, name, desc = row
            print(f"id: {id}, name: {name}, desc: {desc}")

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)


conn.close()