I'm trying to pull back a fairly involved query from mysql - I've got the connection working OK, and can successfully complete the query when I hard code the values.
I'd like to make one of the values a python variable - I am following the method I found here: How to use variables in SQL statement in Python? as the listed usecase seems pretty much the same as mine.
The mysql execute function I am using is different to the solved problem, and I can't figure out where to place the python variable into the execute call.
My code:
targetPUID = "fmt/123"
cur.execute("""
(
SELECT `PUID`,`DROID_V`,`SIG_V`,`SPEED`,
COUNT(distinct IF(sourcelist.hasExtension=1,NAME,NULL)) as Ext,
COUNT(distinct IF(sourcelist.hasExtension=0,NAME,NULL)) as NoExt,
COUNT(distinct NAME) as `All`
FROM sourcelist, main_small
WHERE sourcelist.SourcePUID ="%s" AND main_small.NAME = sourcelist.SourceFileName
GROUP BY `PUID`,`DROID_V`,`SIG_V`,`SPEED` ORDER BY `DROID_V` ASC, `SIG_V`
)
""")
I know I need to place a reference to my variable targetPUID
to link up with the %s
in the query.
___________________FIXED___________________
OK, got it working:
cur.execute("""
(
SELECT `PUID`,`DROID_V`,`SIG_V`,`SPEED`,
COUNT(distinct IF(sourcelist.hasExtension=1,NAME,NULL)) as Ext,
COUNT(distinct IF(sourcelist.hasExtension=0,NAME,NULL)) as NoExt,
COUNT(distinct NAME) as `All`
FROM sourcelist, main_small
WHERE sourcelist.SourcePUID =%s AND main_small.NAME = sourcelist.SourceFileName
GROUP BY `PUID`,`DROID_V`,`SIG_V`,`SPEED` ORDER BY `DROID_V` ASC, `SIG_V`
)
""",targetPUID)
and
targetPUID = "x-fmt/409"
I don't have enough rep to add the fix yet. Thanks for reading.