-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_table_sql_statement.py
70 lines (52 loc) · 2.1 KB
/
create_table_sql_statement.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
import argparse
def write_to_file(filename, text, append=False):
if append:
with open(filename, "a") as f:
# Since text is a list, join it into a string
f.write("\n".join(text))
else:
# If the file does not exist, create it
with open(filename, "w") as f:
f.write("\n".join(text))
def create_table(name, columns, types):
text = []
if len(columns) != len(types):
raise ValueError("There must be the same number of columns and types")
text.append(f"CREATE TABLE {name} ")
text.append("(")
# If there is only one column, do not add a comma
# Otherwise, add a comma after each but the last column
if len(columns) == 1:
text.append(f"{columns[0]} {types[0]}")
else:
for i in range(len(columns)):
if i != len(columns) - 1:
text.append(f"{columns[i]} {types[i]},")
else:
text.append(f"{columns[i]} {types[i]}")
text.append(");")
return text
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate SQL statement for creating a table")
parser.add_argument(
"--name", "-n", help="name of the table", action="store")
parser.add_argument(
"--columns", "-c", nargs="+", help="columns of the table", action="store")
parser.add_argument(
"--types", "-t", nargs="+", help="types of the columns", action="store")
parser.add_argument(
"--append", "-a", help="append to the file", action="store_true")
parser.add_argument(
"--output", "-o", help="output file", action="store")
args = parser.parse_args()
if args.columns is None or args.types is None or args.name is None or args.output is None:
parser.error(
"You must supply a table name, at least one column name, its corresponding type and an output file")
columns = args.columns
types = args.types
text = create_table(args.name, columns, types)
if args.append:
write_to_file(args.output, text, append=True)
else:
write_to_file(args.output, text)