pythonsql-serversqlalchemy

Execute statement with DB-API style bind params


I am learning sqlalchemy and want to delete rows that are older than X number of days counting from today. When I try this:

from datetime import datetime, timedelta

import sqlalchemy

db_con_string = 'Driver={ODBC Driver 17 for SQL Server};Server=tcp...'
connection_url = sqlalchemy.engine.URL.create("mssql+pyodbc",
                                              query={"odbc_connect": db_con_string})
engine = sqlalchemy.create_engine(connection_url)

with engine.begin() as sql_conn:
    command = 'delete myschema.Logs where [DateTimeSent] < ?'
    params = ((datetime.utcnow() + timedelta(days=-90)), )
    sql_conn.execute(sqlalchemy.sql.text(command), params)

I get: sqlalchemy.exc.ArgumentError: List argument must consist only of tuples or dictionaries

My params is clearly a tuple so I am thinking it's something to do with datetime. How can I fix this error?

I use SqlAlchemy 2.0.23


Solution

  • sql_conn.exec_driver_sql(command, params) worked.