“execute statement”中出现意外的关键字参数。

huangapple go评论74阅读模式
英文:

unexpected keyword argument in execute statement

问题

def add_application_to_db(job_id, data):
    with engine.connect() as conn:
        sql = text(
            "INSERT INTO application(id,job_id,fullname,email,linkedin,education,experience,resumeurl) VALUES (:job_id,:fullname,:email,:linkedin,:education,:experience,:resumeurl)"
        )
        conn.execute(
            job_id=job_id,
            fullname=data["fullname"],
            email=data["email"],
            linkedin=data["linkedin"],
            education=data["education"],
            experience=data["experience"],
            resumeurl=data["resumeurl"],
        )
英文:
def add_application_to_db(job_id, data):
    with engine.connect() as conn:
        sql = text(
            "INSERT INTO application(id,job_id,fullname,email,linkedin,education,experience,resumeurl) VALUES (:job_id,:fullname,:email,:linkedin,:education,:experience,:resumeurl)"
        )
        conn.execute(
            job_id=job_id,
            fullname=data["fullname"],
            email=data["email"],
            linkedin=data["linkedin"],
            education=data["education"],
            experience=data["experience"],
            resumeurl=data["resumeurl"],
        )

I am learning flask and using MySQL and sqlalchemy.I am trying to store info from user into the data base (table name = application)
I got following error
>TypeError: Connection.execute() got an unexpected keyword argument 'job_id'

答案1

得分: 0

如果您正在使用SQLAlchemy 2.0,则必须将参数发送为字典,而不是您现在尝试的方式。

def add_application_to_db(job_id, data):
    row = {
        "job_id": job_id,
        "fullname": data["fullname"],
        "email": data["email"],
        "linkedin": data["linkedin"],
        "education": data["education"],
        "experience": data["experience"],
        "resumeurl": data["resumeurl"],
    }
    with engine.connect() as conn:
        sql = text(
            "INSERT INTO application(id,job_id,fullname,email,linkedin,education,experience,resumeurl) VALUES (:job_id,:fullname,:email,:linkedin,:education,:experience,:resumeurl)"
        )
        conn.execute(sql, row)
英文:

If you are using SQLAlchemy 2.0 then you have to send the parameters as a dict instead of what you are trying now.

def add_application_to_db(job_id, data):
    row = {
        "job_id": job_id,
        "fullname": data["fullname"],
        "email": data["email"],
        "linkedin": data["linkedin"],
        "education": data["education"],
        "experience": data["experience"],
        "resumeurl": data["resumeurl"],
    }
    with engine.connect() as conn:
        sql = text(
            "INSERT INTO application(id,job_id,fullname,email,linkedin,education,experience,resumeurl) VALUES (:job_id,:fullname,:email,:linkedin,:education,:experience,:resumeurl)"
        )
        conn.execute(sql, row)

huangapple
  • 本文由 发表于 2023年6月15日 13:56:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定