MySQL Asterisk AGI - ValueError: Could not process parameters
MySQL Asterisk AGI - ValueError: Could not process parameters
所以我已经对这个问题进行了一分钟的故障排除,奇怪的是它似乎 运行 在独立的 Python 脚本中很好,但是当移动到星号 AGI 时我收到以下错误:
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")
下面是 运行 引发此错误的相关数据。
#!/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()
注意:number_br=3,技术上应该是 number_br=%s,但为了排除故障,我替换为静态变量。整个脚本在旧版本的 CentOS 上运行,我正试图让它在 Debian 服务器上运行。
编辑:正如 arheops 适当指出的那样,此查询在 res 变量方面存在问题,因为它不是字符串,解决方案最终是更改变量在查询中的包含方式,请参见下面的示例带来了解决方案
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)
cursor.execute 接受字符串,而不是元组作为参数。
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html
变量 res 根本不是字符串。
所以我已经对这个问题进行了一分钟的故障排除,奇怪的是它似乎 运行 在独立的 Python 脚本中很好,但是当移动到星号 AGI 时我收到以下错误:
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")
下面是 运行 引发此错误的相关数据。
#!/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()
注意:number_br=3,技术上应该是 number_br=%s,但为了排除故障,我替换为静态变量。整个脚本在旧版本的 CentOS 上运行,我正试图让它在 Debian 服务器上运行。
编辑:正如 arheops 适当指出的那样,此查询在 res 变量方面存在问题,因为它不是字符串,解决方案最终是更改变量在查询中的包含方式,请参见下面的示例带来了解决方案
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)
cursor.execute 接受字符串,而不是元组作为参数。
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html
变量 res 根本不是字符串。