forked from CodeYouOrg/horror-movies-c9177d-f34623
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_sql.py
101 lines (80 loc) · 2.82 KB
/
run_sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import sqlite3
from sqlite3 import Error
import pandas as pd
import argparse
import os
# Python script to execute a SQL statement and store the results in a CSV file.
#
# Usage:
# python3 run_sql.py [path_to_db] [path_to_sql] [path_to_csv]
#
# Where:
#
# path_to_db: the path to the sqlite3 database file. default is
# "data/movies.db"
#
# path_to_sql: the path to the file containing the sql query. default is
# "sql/horror_movies.sql"
#
# path_to_csv: the path to the csv file that will be created with the results
# of the query. default is "data/movies.csv"
def get_paths() -> tuple:
"""Get the paths names from the arguments passed in
@return a tuple containing (path_to_db, path_to_sql, path_to_csv)
"""
parser = argparse.ArgumentParser()
parser.add_argument("db", nargs="?",
help="path to the sqlite3 database file",
default="db/movies.db")
parser.add_argument("sql", nargs="?",
help="path to the file containing the sql query",
default="sql/horror_movies.sql")
parser.add_argument("csv", nargs="?",
help="path to the csv file that will be created",
default="data/movies.csv")
args = parser.parse_args()
return args.db, args.sql, args.csv
def create_connection(path_to_db_file: str) -> sqlite3.Connection:
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(path_to_db_file)
return conn
except Error as e:
print(e)
return conn
def get_sql(file_path: str) -> str:
"""retrieve the SQL commands from a text file
@param: file_path - the path to the text file
@return: str - a string containing the contents of the file"""
fd = open(file_path, 'r')
sql = fd.read()
fd.close()
return sql
def main() -> None:
path_to_db, path_to_sql, path_to_csv = get_paths()
conn = create_connection(path_to_db)
sql = get_sql(path_to_sql)
if sql == "-- Add your SQL here" or sql == "":
print("Error: Add your sql to the sql/horror_movies.sql file before running.")
exit(1)
if conn is not None:
movies = pd.read_sql(sql, conn)
if len(movies) == 0:
print("Error: query did not return any results")
exit(1)
csv_dir = os.path.dirname(path_to_csv)
if not os.path.exists(csv_dir):
os.makedirs(csv_dir)
movies.to_csv(path_to_csv, index=False)
else:
print("Error: Could not connect to database.")
exit(1)
conn.close()
return None
if __name__ == "__main__":
main()