Tools, FAQ, Tutorials:
Change Data with "mysql.connector"
How to make data changes to MySQL database with "mysql.connector" module?
✍: FYIcenter.com
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, insert_data.py, that creates a new table, insert a new row of data and commit the changes.
# insert_data.py
# Copyright (c) FYIcenter.com
from mysql.connector import connect
con = connect(host="127.0.0.1", port=3306, \
user="guest", password="retneciyf")
con.cmd_init_db("test")
sql = "CREATE TABLE fyi_links (id INTEGER PRIMARY KEY, \
url VARCHAR(80) NOT NULL, title VARCHAR(1024))"
con.cmd_query(sql)
sql = "INSERT INTO fyi_links (id, url, title) \
VALUES (101, 'dev.fyicenter.com', 'Developer FYI')"
con.cmd_query(sql)
con.commit()
con.close()
2. Run the Python script.
fyicenter> python3 insert_data.py
3. Check the result with the "mysql" client program.
fyicenter> mysql -u guest -pretneciyf -h 127.0.0.1 test mysql> select * from fyi_links; +-----+-------------------+---------------+ | id | url | title | +-----+-------------------+---------------+ | 101 | dev.fyicenter.com | Developer FYI | +-----+-------------------+---------------+ 1 row in set (0.00 sec)
⇒ Handle Exceptions with "mysql.connector"
⇐ "mysql.connector" Module by mysql.com
2021-09-09, ∼1459🔥, 0💬
Popular Posts:
How to use "xsl-transform" Azure API Policy Statement? The "xsl-transform" Policy Statement allows y...
Where to find tutorials on EPUB file format? I want to know how to create EPUB books. Here is a larg...
Can You Add Values to an Array without Keys in PHP? Can You Add Values to an Array with a Key? The a...
Where to get a real Atom XML example? You can follow this tutorial to get a real Atom XML example: 1...
How to validate the id_token signature received from Azure AD v2.0 authentication response? You can ...