import psycopg2
import boto3
stmt = """
SELECT
film.film_id,
title,
inventory_id
FROM
film
LEFT JOIN inventory
ON inventory.film_id = film.film_id
ORDER BY title;
"""
db_conn = psycopg2.connect(host='数据库连接ip', dbname='数据库schema', user='用户名', password='密码')
print('Connect [postgres] database successfully!')
cur = db_conn.cursor()
cur.execute(stmt)
header = [row[0] for row in cur.description]
rows = cur.fetchall()
print(header)
print("------------------")
print(rows)
f = open('task2' + '.csv', 'w')
f.write(','.join(header) + '\n')
for row in rows:
f.write(','.join(str(r) for r in row) + '\n')
f.close()
cur.close()