MySQL Shell CRUD With Python: Update – with examples

Using MySQL Shell in Python mode to CREATE and SELECT records are straight-forward processes using the insert() and select() methods respectively. However, we are fully aware that seldom is the case in which some of that data does not change. So how do we modify or change the data we have on-hand? In SQL, UPDATE takes care of that. We can also apply the same UPDATE command in MySQL Shell Python mode with the update() method. Just as in SQL, you use the SET clause, so too do you use it with update() by way of the set() method. Keep reading to see examples of these methods combined with the where() method for specific, targeted updates…

computer chip board
Photo by Michael Dziedzic on Unsplash

OS, Software, and DB used:

  • OpenSuse Leap 15.1
  • MySQL 8.0.19


Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


First time reading this series on MySQL Shell CRUD with Python? Visit the 2 previous posts to get up to speed:

I’ll use the get_table() method and create an object for the ‘friend’ table, which will be the focus for the examples in this post:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 MySQL  localhost:33060+ ssl  learning  Py > friend = db.get_table('friends')
 MySQL  localhost:33060+ ssl  learning  Py > friend.select()
+---------+------------+-----------+-------+--------------+------------+-----+
| country | first_name | last_name | state | phone_num    | birthday   | age |
+---------+------------+-----------+-------+--------------+------------+-----+
| USA     | Max        | Maxer     | NULL  | 398-392-5656 | 1975-01-23 |  44 |
| CAN     | Mary       | Murphy    | NULL  | 212-543-9420 | 1978-03-23 |  41 |
| USA     | Charlie    | Charles   | NULL  | 888-767-2323 | 1971-08-22 |  48 |
| USA     | Humpty     | Dumpty    | NULL  | 118-257-7344 | 1971-11-22 |  48 |
| USA     | Roger      | Dodger    | NULL  | 234-767-3983 | 1975-08-22 |  44 |
| USA     | Jim        | Russ      | K     | 424-060-3875 | 1975-05-05 |  44 |
| USA     | Jerome     | Harper    | C     | 321-232-9866 | 1976-09-01 |  43 |
| USA     | Jupyter    | Moonbeam  | M     | 198-654-2827 | 1978-07-22 |  41 |
+---------+------------+-----------+-------+--------------+------------+-----+
8 rows in set (0.0629 sec)

There are 8 rows of data currently stored in the ‘friends’ table.

Suppose I want to modify the ‘state’ column, changing the current value to ‘K’ for each row that currently has the NULL marker. In MySQL, I would use the following command:

1
2
3
UPDATE friend
SET state = 'K'
WHERE state IS NULL;

Can I perform this exact same UPDATE in Python mode?

You betcha…

1
2
3
4
 MySQL  localhost:33060+ ssl  learning  Py > friend.update().set('state','K').where('state IS NULL').execute()
Query OK, 5 items affected (0.0973 sec)

Rows matched: 5  Changed: 5  Warnings: 0

We’ve seen the where() method in the previous posts of the series, but, the set() method we have not. Syntax for the set() method is: set(<em>column_name</em>, <em>column_value</em>).

Let’s see those changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 MySQL  localhost:33060+ ssl  learning  Py > friend.select()
+---------+------------+-----------+-------+--------------+------------+-----+
| country | first_name | last_name | state | phone_num    | birthday   | age |
+---------+------------+-----------+-------+--------------+------------+-----+
| USA     | Max        | Maxer     | K     | 398-392-5656 | 1975-01-23 |  44 |
| CAN     | Mary       | Murphy    | K     | 212-543-9420 | 1978-03-23 |  41 |
| USA     | Charlie    | Charles   | K     | 888-767-2323 | 1971-08-22 |  48 |
| USA     | Humpty     | Dumpty    | K     | 118-257-7344 | 1971-11-22 |  48 |
| USA     | Roger      | Dodger    | K     | 234-767-3983 | 1975-08-22 |  44 |
| USA     | Jim        | Russ      | K     | 424-060-3875 | 1975-05-05 |  44 |
| USA     | Jerome     | Harper    | C     | 321-232-9866 | 1976-09-01 |  43 |
| USA     | Jupyter    | Moonbeam  | M     | 198-654-2827 | 1978-07-22 |  41 |
+---------+------------+-----------+-------+--------------+------------+-----+
8 rows in set (0.0009 sec)

What about changing multiple column values in one update() statement?
Totally doable…

Suppose this is the requirement. Our fictitious friend ‘Jerome Harper’ has changed his last name to ‘Harper-Hall’ and moved to Mexico. How can we make these changes for his row in our table using update() and set()?

1
2
3
4
 MySQL  localhost:33060+ ssl  learning  Py > friend.update().set('last_name','Harper-Hall').set('country','MEX').where('first_name = :first_name').bind('first_name','Jerome').execute()
Query OK, 1 item affected (0.1107 sec)

Rows matched: 1  Changed: 1  Warnings: 0

Notice the 2 calls to the set() method, each specifying a column and desired value. Also, in the where() method, I filter rows using the bind() method (explored in the CRUD: Read post, linked above).

In the below query results, we can the update() was successful:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 MySQL  localhost:33060+ ssl  learning  Py > friend.select()
+---------+------------+-------------+-------+--------------+------------+-----+
| country | first_name | last_name   | state | phone_num    | birthday   | age |
+---------+------------+-------------+-------+--------------+------------+-----+
| USA     | Max        | Maxer       | K     | 398-392-5656 | 1975-01-23 |  44 |
| CAN     | Mary       | Murphy      | K     | 212-543-9420 | 1978-03-23 |  41 |
| USA     | Charlie    | Charles     | K     | 888-767-2323 | 1971-08-22 |  48 |
| USA     | Humpty     | Dumpty      | K     | 118-257-7344 | 1971-11-22 |  48 |
| USA     | Roger      | Dodger      | K     | 234-767-3983 | 1975-08-22 |  44 |
| USA     | Jim        | Russ        | K     | 424-060-3875 | 1975-05-05 |  44 |
| MEX     | Jerome     | Harper-Hall | C     | 321-232-9866 | 1976-09-01 |  43 |
| USA     | Jupyter    | Moonbeam    | M     | 198-654-2827 | 1978-07-22 |  41 |
+---------+------------+-------------+-------+--------------+------------+-----+
8 rows in set (0.0010 sec)

That’s pretty much all there is to update(). As with all DML commands, the target rows depend heavily on how you filter them for UPDATE or DELETE. Speaking of DELETE, that is the next MySQL Shell CRUD with Python operation and it is the final post in the series. Be sure to subscribe and check back in when it drops…Thanks for reading!

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Advertisements

One thought on “MySQL Shell CRUD With Python: Update – with examples

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.