Skip to content

Commit 0e8fa9f

Browse files
committed
added execute flag into argparse
1 parent a0cc0fa commit 0e8fa9f

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

src/rowgen/main.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def main():
2020
parser.add_argument("--user")
2121
parser.add_argument("--password")
2222
parser.add_argument("--database")
23+
parser.add_argument(
24+
"--execute",
25+
action="store_true",
26+
help="Executes insert statements into the database directly.",
27+
)
2328

2429
args = parser.parse_args()
2530

@@ -30,18 +35,23 @@ def main():
3035

3136
dbc = DBconnect(db_url)
3237
hf = HFapi()
33-
data = hf.prompt_fake_data(dbc.table_columns, 20)
34-
# print(inserts)
35-
sql_parser = parse_sql_from_code_block(data)
36-
print(f"sqlparserrrrrrrrr:{sql_parser}")
37-
sql_parser = sql_parser.split("\n")
38-
print(sql_parser)
38+
ai_sql_response = hf.prompt_fake_data(dbc.table_columns, 20)
39+
sql_statements = parse_sql_from_code_block(ai_sql_response)
3940
# execute
40-
engine = create_engine("sqlite:///testrowgendb.sqlite") # replace with your DB URL
41-
with engine.connect() as connection:
42-
for sql in sql_parser:
43-
connection.execute(text(sql))
44-
connection.commit()
41+
if args.execute:
42+
sql_statements = sql_statements.split("\n")
43+
engine = create_engine(
44+
"sqlite:///testrowgendb.sqlite"
45+
) # replace with your DB URL
46+
with engine.connect() as connection:
47+
for sql in sql_statements:
48+
connection.execute(text(sql))
49+
connection.commit()
50+
print("Insert statements were executed into the database.")
51+
else:
52+
with open("init_db.sql", "w") as f:
53+
f.write(sql_statements)
54+
print("Saved to sql file.")
4555

4656

4757
if __name__ == "__main__":

0 commit comments

Comments
 (0)