删除列值与Pandas Dataframe列值匹配的SQL表行

时间:2019-09-19 21:00:55

标签: python sqlalchemy

我正在尝试删除SQL表中在熊猫数据框中具有匹配列值的行。这是我到目前为止的代码。由于某些原因,它仅对某些数据有效。我不断收到下面列出的错误。最好的方法是什么?

import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import update
from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy import select
from sqlalchemy import or_
from sqlalchemy import and_

    engine = create_engine(cnxn_str)
    meta = sqlalchemy.MetaData()
    # Map the Inventory table in your database to a SQLAlchemy object
    inventory = sqlalchemy.Table(table_name, meta, autoload=True, autoload_with=engine)
    #make the condition
    cond = inventory.c['column_name'].in_(df['column_name'])
    # Define and execute the DELETE
    delete = inventory.delete().where(cond)
    with engine.connect() as conn:
        conn.execute(delete)

DBAPIError: (pyodbc.Error) ('07002', '[07002] [Microsoft][ODBC Driver 13 for SQL Server]COUNT field incorrect or syntax error (0) (SQLExecDirectW)') [SQL: 'DELETE FROM [table_name] WHERE [table_name].[column_name] IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,

0 个答案:

没有答案