【问题标题】:TypeError: not enough arguments for format string Python3-MySQLTypeError:格式字符串 Python3-MySQL 的参数不足
【发布时间】:2017-12-18 18:05:48
【问题描述】:

我编写了一个网络应用程序,它接受输入并将其存储在 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()

这有效,除非在标记为空的列中有数据。这些列在第一次创建时为空,我会保留一段时间,但其他列中有可搜索的数据。

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

    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,但这只是一个猜测。

【问题讨论】:

标签: python mysql typeerror pymysql


【解决方案1】:

比尔,

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

这是我的工作代码:

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 代码需要这种格式。

【讨论】:

    猜你喜欢
    • 2019-01-17
    • 2018-06-07
    • 1970-01-01
    • 2021-04-01
    • 2012-06-24
    • 2020-10-17
    • 2012-06-11
    • 2016-01-16
    相关资源
    最近更新 更多