Change Data with "mysql.connector"


How to make data changes to MySQL database with "mysql.connector" module?



Making data changes to MySQL database with "mysql.connector" requires to run INSERT, UPDATE or DELETE SQL statements with an established connection.

By default, changes are not committed until you call the con.commit() method explicitly.

1. Create is a sample Python script,, that creates a new table, insert a new row of data and commit the changes.

# Copyright (c) 

from mysql.connector import connect
con = connect(host="", port=3306, \
  user="guest", password="retneciyf")

sql = "CREATE TABLE fyi_links (id INTEGER PRIMARY KEY, \
  url VARCHAR(80) NOT NULL, title VARCHAR(1024))"

sql = "INSERT INTO fyi_links (id, url, title) \
  VALUES (101, '', 'Developer FYI')"


2. Run the Python script.

fyicenter> python3 

3. Check the result with the "mysql" client program.

fyicenter> mysql -u guest -pretneciyf -h test

mysql> select * from fyi_links;
| id  | url               | title         |
| 101 | | Developer FYI |
1 row in set (0.00 sec)


Handle Exceptions with "mysql.connector"

"mysql.connector" Module by

Python Modules for MySQL Database

⇑⇑ Python Tutorials

2021-09-09, 213👍, 0💬