【问题标题】:Concat function returns 1 on MySQL query on FlaskConcat 函数在 Flask 上的 MySQL 查询中返回 1
【发布时间】:2018-10-10 01:33:39
【问题描述】:

我在烧瓶中使用concat 函数运行MySql 查询,它总是返回1。

cur.execute("INSERT INTO jobs( job_id, job_name, company, who_created) 
VALUES(%s,%s, %s,%s)",(job_id,job_name, company,
[cur.execute("select concat(first_name , ' ' , last_name)
from employees where username = %s", [username] ) ] ) )

who_created 总是返回 1。

当我在 MySQLWorkbench 上运行查询时,它工作正常并返回 first name + last name。

问题不在于 concat 函数,因为即使我在没有 concat 的情况下运行它,我也会收到“str object is not callable”错误

谢谢。

【问题讨论】:

    标签: mysql flask mysql-python


    【解决方案1】:

    cur.execute 将返回找到的行数。要获取返回的数据,需要在执行后使用另一个调用:

    cur.execute("select concat(first_name , ' ' , last_name) from employees where username = %s", [username] )
    name_rs = cur.fetchall()
    cur.execute("INSERT INTO jobs( job_id, job_name, company, who_created) VALUES(%s,%s,%s,%s)",(job_id, job_name, company, name_rs[0][0] ) )
    

    【讨论】:

    • 当我运行你的代码时,我得到 builtins.KeyError KeyError: 0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2019-02-25
    • 1970-01-01
    • 2020-08-04
    • 2014-02-15
    • 1970-01-01
    • 2017-10-08
    相关资源
    最近更新 更多