CSVAgent / database.py
Quazim0t0's picture
Update database.py
c5cf00a verified
raw
history blame
627 Bytes
@tool
def sql_engine(query: str) -> str:
"""
Executes an SQL SELECT query and returns the results.
Parameters:
query (str): The SQL query string to execute. Only SELECT queries are allowed.
Returns:
str: A formatted string containing the query results, or an error message if the query fails.
"""
try:
with engine.connect() as con:
rows = con.execute(text(query)).fetchall()
if not rows:
return "No results found."
return "\n".join([", ".join(map(str, row)) for row in rows])
except Exception as e:
return f"Error: {str(e)}"