简体   繁体   English

使用 python 在 mysql 查询结果上添加字符串的问题

[英]problems to add a string on mysql query result with python

I want to add a string to the result of my query in mysql.我想在 mysql 中的查询结果中添加一个字符串。

I tried this code:我试过这段代码:

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)

the error is this错误是这样的

mysql.connector.errors.ProgrammingError: Parameters for query must be list or tuple. mysql.connector.errors.ProgrammingError:查询的参数必须是列表或元组。

i want the results looks like this我希望结果看起来像这样

| 56953404789 | some message  |

Any idea what is happening to cause my error?知道发生了什么导致我的错误吗?

Thanks in advance for any help提前感谢您的帮助

EDIT: Provided the injection safe method:编辑:提供注射安全方法:


Use the sql CONCAT function:使用 sql CONCAT function:

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

Note that on sqlite there is no CONCAT function, instead use the ||请注意,在 sqlite 上没有 CONCAT function,而是使用 || operator:操作员:

"SELECT msisdn || ? AS mensaje FROM perfiles"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM