我编写了一个Web应用程序,它接受输入并将其存储在MySQL数据库中。我在Web应用程序上创建了一个搜索字段,以便可以输入一个字符串,它将搜索所有列以获取数据输入并将其返回。
最初我的搜索pymysql脚本看起来像这样。
try:
with connection.cursor(pymysql.cursors.DictCursor) as cursor:
sql = """SELECT * FROM main WHERE Concat(idDirectConnect_ID,
CR_Number,
VPC_Name,
Creator_Name,
Route_Domain,
OSPF_ASN,
OSPF_VLAN,
OSPF_Subnet,
BGP_VLAN,
BGP_Subnet,
BGP_AuthKey is null,
AWS_Expected_Subnet,
AWS_Acnt,
Company_Name,
Lpbck_Int,
Lpbck_IPAddr,
Tunnel_Num1,
Tunnel_Num2,
VPN_Tunnel1_Dest is null,
VPN_Int1_CryptoKey is null,
VPN_Tun1_IP is null,
VPN_Tunnel2_Dest is null,
VPN_Int2_CryptoKey is null,
VPN_Tun2_IP is null,
AWS_VPN_ConnectionID is null)
LIKE %s;"""
cursor.execute(sql, ('%{}%'.format(sql_var)))
result = cursor.fetchall()
return result
finally:
connection.close()
除非标记的列中的数据为null,否则此方法有效。这些列在第一次创建时为空,并且我保持一段时间,但是其他列中有可搜索的数据。
因此,我正在尝试将搜索查询更新为:
try:
with connection.cursor() as cursor:
sql = """SELECT * FROM main WHERE (idDirectConnect_ID LIKE %s
OR CR_Number LIKE %s
OR VPC_Name LIKE %s
OR Creator_Name LIKE %s
OR Route_Domain LIKE %s
OR OSPF_ASN LIKE %s
OR OSPF_VLAN LIKE %s
OR OSPF_Subnet LIKE %s
OR BGP_VLAN LIKE %s
OR BGP_Subnet LIKE %s
OR AWS_Expected_Subnet LIKE %s
OR AWS_Acnt LIKE %s
OR Company_Name LIKE %s
OR Lpbck_Int LIKE %s
OR Lpbck_IPAddr LIKE %s
OR Tunnel_Num1 LIKE %s
OR Tunnel_Num2 LIKE %s
OR BGP_AuthKey LIKE %s
OR VPN_Tunnel1_Dest LIKE %s
OR VPN_Int1_CryptoKey LIKE %s
OR VPN_Tun1_IP LIKE %s
OR VPN_Tunnel2_Dest LIKE %s
OR VPN_Int2_CryptoKey LIKE %s
OR VPN_Tun2_IP LIKE %s
OR AWS_VPN_ConnectionID LIKE %s);"""
cursor.execute(sql, ", ".join(["'%{}%'".format(sql_var)]*25))
result = cursor.fetchall()
return result
finally:
connection.close()
但我得到了:
query = query % self._escape_args(args, conn)
TypeError: not enough arguments for format string
我最初只有:
cursor.execute(sql, ('%{}%'.format(sql_var)))
但是我意识到所有%s都没有足够的参数,所以我将cursor.execute改为:
cursor.execute(sql, ", ".join(["'%{}%'".format(sql_var)]*25))
现在回归:
"'%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%', '%206%'"
我已经对他们进行了统计并对其进行了叙述,并且我已经计算并叙述了%s并且应该已经足够了。
我已经阅读了尽可能多的类似SO帖子和MySQL文档。此时我怀疑我可能需要将光标更改为executemany,但这只是猜测。
答案 0 :(得分:0)
比尔,
谢谢,这使我指出了正确的方向。
这是我的工作代码:
try:
with connection.cursor(pymysql.cursors.DictCursor) as cursor:
sql = """SELECT * FROM main WHERE (idDirectConnect_ID LIKE %s
OR CR_Number LIKE %s
OR VPC_Name LIKE %s
OR Creator_Name LIKE %s
OR Route_Domain LIKE %s
OR OSPF_ASN LIKE %s
OR OSPF_VLAN LIKE %s
OR OSPF_Subnet LIKE %s
OR BGP_VLAN LIKE %s
OR BGP_Subnet LIKE %s
OR AWS_Expected_Subnet LIKE %s
OR AWS_Acnt LIKE %s
OR Company_Name LIKE %s
OR Lpbck_Int LIKE %s
OR Lpbck_IPAddr LIKE %s
OR Tunnel_Num1 LIKE %s
OR Tunnel_Num2 LIKE %s
OR BGP_AuthKey LIKE %s
OR VPN_Tunnel1_Dest LIKE %s
OR VPN_Int1_CryptoKey LIKE %s
OR VPN_Tun1_IP LIKE %s
OR VPN_Tunnel2_Dest LIKE %s
OR VPN_Int2_CryptoKey LIKE %s
OR VPN_Tun2_IP LIKE %s
OR AWS_VPN_ConnectionID LIKE %s);"""
cursor.execute(sql, list(['%{}%'.format(sql_var)]*25))
result = cursor.fetchall()
return result
finally:
connection.close()
我还必须将光标更改回DictCursor,因为其余的python代码需要格式化。