我正在尝试将PHP查询从PHP转换为MySQLdb for Python。
以下是PHP代码:
$query = "
select
`ip_address`.`address`,
`interface`.`name`,
`equipment`.`dns_hostname`,`interface`.`vrf_id`
from
`equipment` `equipment`
inner join `tm_se_equipment` `tm_se_equipment`
on `equipment`.`id` = `tm_se_equipment`.`equipment_id`
inner join `interface` `interface`
on `interface`.`equipment_id` = `equipment`.`id`
inner join `ip_address` `ip_address`
on `ip_address`.`if_id` = `interface`.`id`
where
(`ip_address`.`address` = '$ip_address')
";
我试图像这样翻译但是没有用,我获取的唯一结果是()
:
ip_address = ipaddr.IPAddress(linknet)
linknet = "1.1.1.1"
cursor.execute("""SELECT `ip_address`.`address`, `interface`.`name`, `equipment`.`dns_hostname`, `interface`.`vrf_id` FROM `equipment` `equipment` INNER JOIN `tm_se_equipment` `tm_se_equipment` ON `equipment`.`id` = `tm_se_equipment`.`equipment_id` INNER JOIN `interface` `interface` ON `interface`.`equipment_id` = `equipment`.`id` INNER JOIN `ip_address` `ip_address` ON `ip_address`.`if_id` = `interface`.`id` WHERE `ip_address`.`address` = %s""", (ip_address,))
答案 0 :(得分:0)
试试这个:
cursor.execute("""SELECT `ip_address`.`address`, `interface`.`name`, `equipment`.`dns_hostname`, `interface`.`vrf_id` FROM `equipment` `equipment` INNER JOIN `tm_se_equipment` `tm_se_equipment` ON `equipment`.`id` = `tm_se_equipment`.`equipment_id` INNER JOIN `interface` `interface` ON `interface`.`equipment_id` = `equipment`.`id` INNER JOIN `ip_address` `ip_address` ON `ip_address`.`if_id` = `interface`.`id` WHERE `ip_address`.`address` = '%s'""" % ip_address)
或者,如果您尝试将ip地址作为执行函数的参数,则需要使用“?”而不是“%”