String Replacement Problems In Mysqldb.exeute
I have a small problem. And i think I am f=doing something silly but can not figure it out first : i want to fire a dynamically constructed sql on mysql from python. Its simple in
Solution 1:
You want to pass the arguments using
cur.execute(stmt, v)
and not
cur.execute(stmt % v)
If you have
"INSERT INTO tab (col1, col2) VALUES (%s, %s)"
and you pass two strings ('val1', 'val2')
using %
, it will become:
"INSERT INTO tab (col1, col2) VALUES (val1, val2)"
which is interpreted as column names, which obviously do not exist.
If you do
cur.execute(stmt, v)
it will be interpreted correctly as
"INSERT INTO tab (col1, col2) VALUES ('val1', 'val2')"
You can also use
print cur.mogrify(stmt, v)
to see the correctly formatted query as it could be executed in your DB.
Post a Comment for "String Replacement Problems In Mysqldb.exeute"