sqlalchemy + pandas 参数化查询字符串执行错误

问题描述 投票:0回答:1
import pandas as pd
from sqlalchemy import create_engine

host = "xxxx.us-west-2.rds.amazonaws.com"
user = "user1"
pwd = "xxx"
database = "db1"


engine = create_engine("mysql+pymysql://{}:{}@{}/{}".format(user,pwd,host,database), echo=True)


query1 = """
         select * 
         from tbl1 
         where dt <= :cutoff_dt
         """


params={
        "cutoff_dt": "2023-9-30"
       }

# Begin transaction and create session
with engine.begin() as conn:
    try:
        # Execute your query
        df1 = pd.read_sql(
                  query1, 
                  conn, 
                  params=params
                 )

    except Exception as e:
        # Handle any errors
        print(f"Error occurred: {e}")
        conn.rollback()  # Rollback transaction if error occurs

    finally:
        # Close connection and rollback if not explicit (safer)
        conn.close()  # Explicitly close the connection even if no error

错误:

[参数:{'cutoff_dt':'2023-9-30'}] (此错误的背景位于:https://sqlalche.me/e/20/f405) 2024-02-13 11:14:16,745 信息 sqlalchemy.engine.Engine 回滚

python pandas sqlalchemy
1个回答
0
投票

问题在于参数占位符 (

:cutoff
)。它按原样传递给
pymysql
,它不会将其识别为占位符,因此不会将其替换为参数值。

您可以:

  • 使用pymysql的格式化:

    query1 = """SELECT * FROM tbl1 WHERE dt <= %(cutoff)s"""
    
  • 用 sqlalchemy.text 包装查询,以便正确处理

    :cutoff

    query1 = sqlalchemy.text("""SELECT * FROM tbl1 WHERE dt <= :cutoff""")
    
© www.soinside.com 2019 - 2024. All rights reserved.