2022-07-17 14:27:15 +00:00
|
|
|
import argparse
|
|
|
|
import sqlite3
|
|
|
|
from tabulate import tabulate
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
"""
|
|
|
|
Parses the arguments from the CLI
|
|
|
|
"""
|
|
|
|
parser = argparse.ArgumentParser(description="Testtool Adress Changes")
|
|
|
|
parser.add_argument(
|
|
|
|
'-c', '--create',
|
|
|
|
help='Creates an example database',
|
|
|
|
action='store_true'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-i', '--insert',
|
|
|
|
help='insert some example data',
|
|
|
|
action='store_true'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-p', '--print',
|
|
|
|
choices=['table', 'string'],
|
|
|
|
default='table',
|
|
|
|
help='prints all the cells from the table',
|
|
|
|
)
|
2022-07-17 21:57:13 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-u', '--update',
|
|
|
|
action='store_true',
|
|
|
|
help='Updates the city names'
|
|
|
|
)
|
2022-07-17 14:27:15 +00:00
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def execute_sql(args):
|
|
|
|
db = sqlite3.connect("test.db")
|
|
|
|
cur = db.cursor()
|
|
|
|
print(args.print)
|
|
|
|
if args.create:
|
|
|
|
print("Creating")
|
|
|
|
with open ('sql/create_db.sql', 'r') as sql_file:
|
|
|
|
cur.executescript(sql_file.read())
|
|
|
|
elif args.insert:
|
|
|
|
print("insert")
|
|
|
|
with open ('sql/insert_data.sql', 'r') as sql_file:
|
|
|
|
cur.executescript(sql_file.read())
|
2022-07-17 21:57:13 +00:00
|
|
|
elif args.update:
|
|
|
|
print("Updating database")
|
|
|
|
with open ('sql/update_sg.sql', 'r') as sql_file:
|
|
|
|
cur.executescript(sql_file.read())
|
|
|
|
|
2022-07-17 14:27:15 +00:00
|
|
|
elif args.print:
|
|
|
|
cur.execute("SELECT * from customers")
|
|
|
|
if args.print == 'table':
|
|
|
|
print(tabulate(cur.fetchall()))
|
|
|
|
else:
|
|
|
|
print(cur.fetchall())
|
|
|
|
db.commit()
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = parse_args()
|
|
|
|
execute_sql(args)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|