python mysql参数化查询_pymysql:MySQL参数化查询

在Python中使用pymysql进行MySQL参数化查询时遇到了TypeError,问题出在LIKE查询的参数化方式。示例代码中,尝试通过格式化字符串创建LIKE条件,但执行时发生错误。正确的做法是使用问号(?)作为占位符,并在执行查询时传递参数列表。修复后的代码应能正确执行LIKE查询,例如:`SELECT name, source, destination FROM mytable WHERE (source LIKE ? OR destination LIKE ?);`,参数为`['%74.121.242.2%', '%74.121.242.2%']`。" 95042308,8362225,创建WPF登录界面带动画效果,"['前端开发', 'WPF', 'UI设计', '动画']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为什么我在尝试执行此查询时得到TypeError: not all arguments converted during string formatting?我需要能够将%{}%追加到传入的IP上,这样我就可以运行LIKEmysql查询。在

如果这不是使用%通配符参数化LIKE查询的正确方法,那么该怎么做?在

班级:class IpCleaner(object):

def __init__(self, ip):

self.ip = ip

self.iplike = '%{}%'.format(self.ip)

def lookup(self):

self.dbconnect()

select_query = (

"SELECT `name`,`source`,`destination` FROM mytable "

"WHERE (`source` LIKE ? OR `destination` ? );"

)

params = [self.iplike, self.iplike]

print params

self.cur.execute(select_query, params)

print self.cur.fetchall()

实例化:

^{pr2}$

输出:Traceback (most recent call last):

['%74.121.242.2%', '%74.121.242.2%']

File "/home/dobbs/shunlibs/IpCleaner.py", line 87, in

a.palorulelookup()

File "/home/dobbs/shunlibs/IpCleaner.py", line 81, in lookup

self.cur.execute(select_query, params)

File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 164, in execute

query = self.mogrify(query, args)

File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 143, in mogrify

query = query % self._escape_args(args, conn)

TypeError: not all arguments converted during string formatting

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值