Search code examples
pythonmysqlasteriskagi

MySQL Asterisk AGI - ValueError: Could not process parameters


So I've been troubleshooting this one for a minute, what's weird is that it seems to run just fine in a stand alone Python script, but when being moved to an asterisk AGI I get the below error:

cursor.execute(query, (res))
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor_cext.py", line 248, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection_cext.py", line 615, in prepare_for_mysql
raise ValueError("Could not process parameters")

Below is the data in question that was ran that threw this error.

#!/usr/bin/env python
import sys
from asterisk.agi import *
from os import system
import subprocess
import time
import mysql.connector
import md5

agi = AGI()
agi.verbose("python agi started")
callerId = agi.env['agi_callerid']
agi.verbose("call from %s" % callerId)
res=agi.get_variable("res")
agi.verbose("want %s br" %res)

cnx = mysql.connector.connect(user='user',password='sanitized',host='127.0.0.1',database='rental')

cursor = cnx.cursor(buffered=True)

query = ("select active,address,rent,deposit,phone,pets_allow,smoker_allow,section_8,garage,attached_garage,fenced_yard,carport,shed,patio,deck,basement,garden_area,off_street,on_street,walkin_shower,double_lavs,back_porch,front_porch,cha,ac,fireplace,central_heat,stove,dish_washer,microwave,refrigerator,washer,dryer,washer_hu,dryer_hu,garbage_dispsal,water,trash_service,gas,electric,barn,wall_furnace from activelisting where active=1 and number_br=3")
query = ("select active,address,rent,deposit,phone,calldialog,id from activelisting where active=1 and number_br=3 order by id")

cursor.execute(query, (res))

cursor.close()

cnx.close()

Note: number_br=3, should technically be number_br=%s but for the sake of troubleshooting I substituted in a static variable. This whole script worked on an older version of CentOS and I'm attempting to get it all working on a Debian Server.

EDIT: As arheops appropriately noted, this query was having issues with the res variable, as it was not a string, the resolution was ultimately to change how the variable was included in the query, see below for an example that brought about the solution

query = ("select active,address,rent,deposit,phone,pets_allow,smoker_allow,section_8,garage,attached_garage,fenced_yard,carport,shed,patio,deck,basement,garden_area,of$
query = ("select active,address,rent,deposit,phone,calldialog,id from activelisting where active=1 and number_br={} order by id".format(res))

cursor.execute(query)

Solution

  • cursor.execute accepting string, not touple as param.

    https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html

    Variable res is not string at all.