Categories
Python Answers

How to make MySQL parameterized queries with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *