【问题标题】:problems to add a string on mysql query result with python使用python在mysql查询结果上添加字符串的问题
【发布时间】:2020-03-03 02:45:14
【问题描述】:

我想在mysql中的查询结果中添加一个字符串。

我试过这段代码:

mensaje = input ("introduzca el sms que desea enviar: ")
cursorsql1 = conectar.cursor()
sql = "select msisdn, '(mensaje)'VALUES(%s) as mensaje from perfiles where tarifa not like '%Iot%' and transparent_properties not like '%Momo%' and state like '%active%' and msisdn like '56943404789' and created_at between subdate(current_date, 1) and current_date".format(mensaje, values)
cursorsql1.executemany(sql,mensaje)
columnas = cursorsql1.fetchall()
print(columnas)

错误是这样的

mysql.connector.errors.ProgrammingError: 查询参数必须是列表或元组。

我希望结果看起来像这样

| 56953404789 | some message  |

知道发生了什么导致我的错误吗?

提前感谢您的帮助

【问题讨论】:

  • 尝试将您的参数作为一个列表:cursorsql1.executemany(sql, [mensaje])
  • mysql.connector.errors.ProgrammingError:执行操作失败;无法处理参数:(

标签: python mysql string list executemany


【解决方案1】:

编辑: 提供注射安全方法:


使用sql CONCAT函数:

sql = "SELECT CONCAT(msisdn, ?) AS mensaje FROM perfiles"
cursorsql1.execute(sql, (mensaje,))

请注意,在 sqlite 上没有 CONCAT 函数,而是使用 ||运营商:

"SELECT msisdn || ? AS mensaje FROM perfiles"

【讨论】:

  • thankkkkss 以这种方式工作:“SELECT msisdn, '{}' as mensaje from....”.format(mensaje)
  • 如果您将其设计为供他人使用是非常危险的记录,它很容易受到 SQL 注入攻击。
  • 感谢您的警告,但它仅供我个人使用:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多