Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TypeError:格式字符串Python3 MySQL的参数不足_Python_Mysql_Typeerror_Pymysql - Fatal编程技术网

TypeError:格式字符串Python3 MySQL的参数不足

TypeError:格式字符串Python3 MySQL的参数不足,python,mysql,typeerror,pymysql,Python,Mysql,Typeerror,Pymysql,我已经编写了一个web应用程序,它接受输入并将其存储在MySQL数据库中。我在web应用程序上创建了一个搜索字段,这样就可以输入一个字符串,它将在所有列中搜索数据输入并返回它 最初,我用于搜索的pymysql脚本如下所示 try: with connection.cursor(pymysql.cursors.DictCursor) as cursor: sql = """SELECT * FROM main WHERE Concat(idDirectConnect_I

我已经编写了一个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()
除非标记为空的列中有数据,否则此操作有效。这些列在第一次创建时为空,my在一段时间内保持为空,但是其他列中有可搜索的数据

因此,我尝试将搜索查询更新为:

    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,应该足够了

我已经阅读了尽可能多的类似文章和MySQL文档。此时,我怀疑可能需要将光标更改为ExecuteMay,但这只是猜测。

比尔

谢谢,这为我指明了正确的方向

这是我的工作代码:

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代码的其余部分需要这种格式。

在“Now it's returning”下面有一个字符串。如果查看,您将看到
execute
的第二个参数应该是元组或字典。