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:
Andrew Gleave
2022-11-29 16:28:45 +00:00
committed by GitHub
parent d368c43648
commit ea67c049f0
3 changed files with 42 additions and 3 deletions

View File

@@ -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 ""