首页 > 解决方案 > Concat 函数在 Flask 上的 MySQL 查询中返回 1

问题描述

MySql在带有函数的烧瓶中运行查询,concat它总是返回 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”错误

谢谢。

标签: mysqlflaskmysql-python

解决方案


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] ) )

推荐阅读