#!/usr/bin/python

#print "Content-type: text/html\n\n";

import psycopg2
import os

db_conn = psycopg2.connect(host='localhost', dbname='rmbt', user='rmbt', password='rmbt')
db_cursor = db_conn.cursor()

sql = "COPY (SELECT * FROM radio_signal) TO STDOUT WITH (FORMAT CSV, DELIMITER ',', HEADER)"
with open("/var/lib/tomcat8/webapps/download/data/signal.csv", "w+") as file:
    db_cursor.copy_expert(sql, file)

os.chmod("/var/lib/tomcat8/webapps/download/data/signal.csv", 0o777)
db_conn.close()

print("Databaza uspesne vygenerovana")
