mirror of
https://github.com/hwchase17/langchain.git
synced 2025-10-29 06:41:10 +00:00
Support SQL statements that return no results (#222)
Adds support for statements such as insert, update etc which do not return any rows. `engine.execute` is deprecated and so execution has been updated to use `connection.exec_driver_sql` as-per: https://docs.sqlalchemy.org/en/14/core/connections.html#sqlalchemy.engine.Engine.execute
This commit is contained in:
@@ -66,6 +66,14 @@ class SQLDatabase:
|
||||
return "\n".join(tables)
|
||||
|
||||
def run(self, command: str) -> str:
|
||||
"""Execute a SQL command and return a string of the results."""
|
||||
result = self._engine.execute(command).fetchall()
|
||||
return str(result)
|
||||
"""Execute a SQL command and return a string representing the results.
|
||||
|
||||
If the statement returns rows, a string of the results is returned.
|
||||
If the statement returns no rows, an empty string is returned.
|
||||
"""
|
||||
with self._engine.connect() as connection:
|
||||
cursor = connection.exec_driver_sql(command)
|
||||
if cursor.returns_rows:
|
||||
result = cursor.fetchall()
|
||||
return str(result)
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user