Menu Close

How to make MySQL parameterized queries with Python?

Sometimes, we want to make MySQL parameterized queries with Python.

In this article, we’ll look at how to make MySQL parameterized queries with Python.

How to make MySQL parameterized queries with Python?

To make MySQL parameterized queries with Python, we can call cursor’s execute method.

For instance, we write

c.execute("SELECT * FROM foo WHERE bar = %s AND baz = %s", (param1, param2))

to run a select query with a where clause with the MySQLdb library’s cursor’s execute method.

%s is a placeholder for the parameters that are stored in the tuple.

Conclusion

To make MySQL parameterized queries with Python, we can call cursor’s execute method.

Posted in Python, Python Answers