From d324ddb3c9bda744caedc637c3941117704fe3ba Mon Sep 17 00:00:00 2001 From: caminsha Date: Sun, 17 Jul 2022 16:27:15 +0200 Subject: [PATCH] Started python script --- requirements.txt | 1 + start.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 requirements.txt create mode 100644 start.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6ba386e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +tabulate==0.8.10 diff --git a/start.py b/start.py new file mode 100644 index 0000000..dfe9c9f --- /dev/null +++ b/start.py @@ -0,0 +1,59 @@ +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', + ) + + 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()) + 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()