File size: 627 Bytes
c5cf00a
 
add59ac
c5cf00a
add59ac
c5cf00a
 
037ba4c
c5cf00a
 
add59ac
c5cf00a
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@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)}"