Add the ability to execute any SQL statement.
Reference
Starter code
@app.command()defsql(
sql_query: str=typer.Argument(..., help="SQL query string."),
database_url: Optional[str] =typer.Option(None, "--database-url", "-d", help=database_url_help),
models_path: Optional[str] =typer.Option(None, "--models-path", "-m", help=models_path_help),
):
"""Execute arbitrary SQL code. Execute any SQL string. Examples ```bash sqlcli sql "SELECT * FROM athlete" sqlcli sql 'INSERT INTO athlete (name, sport_id) VALUES ("Jenny", 1)' ``` """models, url, engine, tables=sqlmodel_setup(models_path, database_url)
withengine.connect() asconn:
statement=text(sql_query)
statement=sql_queryinspect(statement, title='statement')
result=conn.execute(statement)
inspect(result, title='result')
ifresult.returns_rows:
data=result.all()
console.print(data)
elifresult.is_insert:
console.print("Writing new data to database")
conn.commit()
Add the ability to execute any SQL statement.
Reference
Starter code