python - How to pass a variable to a mysql query? -
this 1 has no errors
cursor.execute ("update `1` set `2`='aaa' `1`='5'")
this 1 has errors.
cursor.execute ("update `1` set `2`=aaa `1`='5'")
the difference is, trying pass variable 'aaa' it.
here error_log of apache
[tue may 14 21:20:24 2013] [error] [client 127.0.0.1] operationalerror: (1054, "unknown column 'aaa' in 'field list'")
in php type $aaa
in mysql query, assumption in python type aaa
.
if trying pass value of variable, need write "aaa" outside string. so, instead of:
cursor.execute ("update `1` set `2`=aaa `1`='5'")
try:
cursor.execute ("update `1` set `2`= '" + aaa + "' `1`='5'")
just aware that, if using python, may need convert variable string (if holds number example), using:
str(aaa)
Comments
Post a Comment