sql formatter

 05-01-2022

SQL CODE INSERT FORMATTER

This script is written to format attributes from a table.

Example usage

Table only        [1]
Table with column [2]

Enter table type : 1
Name of the table: Baz
How many columns : 2
Enter attribute 1: Foobar
Enter datatype  1: str
Enter attribute 2: 100
Enter datatype  2: int

Output

INSERT INTO BAZ
VALUES ('Foobar', 100)

Code

intro = """
           _   _                     _
 ___  __ _| | (_)_ __  ___  ___ _ __| |_
/ __|/ _` | | | | '_ \/ __|/ _ \ '__| __|
\__ \ (_| | | | | | | \__ \  __/ |  | |_
|___/\__, |_| |_|_| |_|___/\___|_|   \__|
        |_|
  __                            _   _
 / _| ___  _ __ _ __ ___   __ _| |_| |_ ___ _ __
| |_ / _ \| '__| '_ ` _ \ / _` | __| __/ _ \ '__|
|  _| (_) | |  | | | | | | (_| | |_| ||  __/ |
|_|  \___/|_|  |_| |_| |_|\__,_|\__|\__\___|_|

A cli app for formatting attributes to sql insert code.

"""
print(intro)

def table_formatter(list_value, table_name):
    print(f"\nINSERT INTO {table_name.upper()}")
    print(f"VALUES ({(str(list_value).replace('[', '').replace(']', ''))})")


def table_column_formatter(list_value, table_name, column_name):
    f_col_name = ", ".join(column_name)
    print(f"\nINSERT INTO {table_name.upper()} ({f_col_name})")
    print(f"VALUES ({(str(list_value).replace('[', '').replace(']', ''))})")

print("Table only        [1]\nTable with column [2] ")

while True:
    try:
        table_type = input("\nEnter table type : [1] [2] ")
        if table_type == "1":
            break
        elif table_type == "2":
            break
        else:
            print("Not a table type, please select between 1 or 2")
    except ValueError:
        continue

table = input("Name of the table: ")
while True:
    try:
        col_count = int(input("How many columns : "))
        break
    except ValueError:
        print("Not an number, please input a number")
        continue

col = []
col_with_name = []

for i in range(1, col_count + 1):
    if table_type == "1":
        data = input(f"Enter attribute {i}: ")
    elif table_type == "2":
        col_name = input(f"Enter column name {i}: ")
        col_with_name.append(col_name)
        data = input(f"Enter attribute   {i}: ")
    while True:
        data_type = input(f"Enter datatype  {i}: (int) (float) (str) ")
        lower_data_type = data_type.lower()
        if lower_data_type == "int":
            conv_i = int(data)
            col.append(conv_i)
            break
        elif lower_data_type == "float":
            conv_f = float(data)
            col.append(conv_f)
            break
        elif lower_data_type == "str":
            conv_s = str(data)
            col.append(conv_s)
            break
        else:
            print("Wrong datatype, please reenter")

if table_type == "1":
    table_formatter(col, table)
if table_type == "2":
    table_column_formatter(col, table, col_with_name)

input("\nPress any key to exit.")