PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Databases » Python Update MySQL Table

Python Update MySQL Table

Updated on: March 9, 2021 | 14 Comments

This article demonstrates how to execute a MySQL UPDATE query from Python to modify the MySQL table’s data.

Goals of this lesson. You’ll learn the following MySQL UPDATE operations from Python using a ‘MySQL Connector’ module.

  • Update single and multiple rows, single and multiple columns
  • Use a Python variable in a parameterized query to update table rows.
  • Also, Update a column with date-time and timestamp values
  • The role of commit and rollback in the update operation.

Further Reading:

  • Solve Python MySQL Exercise
  • Read Python MySQL Tutorial (Complete Guide)

Table of contents

  • Prerequisite
  • Example to Update a row of MySQL Table
  • Use a Python variable in MySQL Update query
  • Update Multiple Rows of MySQL Table using Python
    • Python update multiple Columns of MySQL table
  • Update Datetime and timestamp column of a MySQL table from Python
  • Next Steps

Prerequisite

Before executing the following program, make sure you have the following in place −

  • Username and password that you need to connect MySQL
  • MySQL database table name which you want to update.

For this article, I am using a Laptop table present in my MySQL server.

If a table is not present in your MySQL server, you can refer to our article to create a MySQL table from Python.

You can also download a SQL query file, which contains SQL queries for table creation and data so that you can use this table for your UPDATE operations.