Search code examples
pythonmysqlstringlistexecutemany

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.

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.

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


Solution

  • EDIT: Provided the injection safe method:


    Use the 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 || operator:

    "SELECT msisdn || ? AS mensaje FROM perfiles"