<jupyter_start><jupyter_text>Accessing the Database<jupyter_code>import psycopg2
import pandas as pd
# ensure all columns are displayed when viewing a pandas dataframe
pd.set_option('display.max_columns', None)
def make_csv(query, filename):
    """ I think it only works for 'SELECT *' statements. """
    # Creating a connection to the database
    print("creating conecction...")
    con = psycopg2.connect(database="mid_term_project", 
                           user="lhl_student", 
                           password="<PASSWORD>", 
                           host="mid-term-project.ca2jkepgjpne.us-east-2.rds.amazonaws.com", 
                           port="5432")


    # creating a cursor object
    cur = con.cursor()
    # running an sql query
    print("running query...")
    cur.execute(query)
    # Storing the result
    rows = cur.fetchall()
    cols = [desc[0] for desc in cur.description]

    con.close()

    # writing the csv file
    print("writing file...")
    df = pd.DataFrame(rows, columns=cols)
    df.to_csv(filename, index=False)
    
    print("done")
make_csv("SELECT * FROM flights_test", "flights_test.csv")
make_csv("SELECT * FROM flights LIMIT 100", "test_csv4.csv")
make_csv("SELECT * FROM flights WHERE fl_date SIMILAR TO '201[8-9]-01-0[1-7]'", "flights_week1_jan.csv")
### This query pulls a random sample
"SELECT * FROM flights WHERE random() < 0.0001"<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12219101123595505, 'fraction_numerical': 0.020365168539325844, 'mean_word_length': 2.925619834710744, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '72992', 'license': 'MIT', 'n_tokens_mistral': 464, 'n_tokens_neox': 437, 'n_words': 113, 'path': 'fetching_data.ipynb', 'repo_name': 'ZacharyArgentin/LHL_Midterm_Project_Predicting_Flight_Delays', 'size': '1425'}