-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_queries.py
54 lines (46 loc) · 1.42 KB
/
run_queries.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
""" Runs all sql queries, that can be found in a directory
"""
import csv
import sqlite3
import sys
import os
import glob
from configs.config_queries import DATABASE
from configs.config_queries import SQL_DIRECTORY
from errros import FilesNotFoundException
def find_sqls(directory):
os.chdir(directory)
res = glob.glob("*.sql")
if len(res) == 0:
raise FilesNotFoundException("Folder does not cointain any *.sql files")
return res
def create_statistic():
""" Executes and prints all queries
"""
files = find_sqls(SQL_DIRECTORY)
con = None
con = database_connect(DATABASE)
cur = con.cursor()
for name in files:
with open(os.path.join(SQL_DIRECTORY, name), "r") as f:
script = f.read()
execute_query(cur, name, script)
if con:
con.close()
def database_connect(file_database):
con = None
try:
con = sqlite3.connect(file_database)
return con
except sqlite3.Error as e:
print("ERROR: {}".format(e))
print("File was {}".format(file_database))
def execute_query(cursor, name, script):
"""Merges the table credentials of the given database with the local database."""
try:
for row in cursor.execute(script):
print("{} --> {}".format(name, row))
except sqlite3.Error as e:
print("ERROR in {}: {}".format(name, e))
if __name__ == '__main__':
create_statistic()