Tools, FAQ, Tutorials:
Change Data with PyMySQL Package
How to make data changes to MySQL database with PyMySQL package?
✍: FYIcenter.com
Making data changes to MySQL database with PyMySQL package 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, PyMySQL_insert_data.py, that creates a new table, insert a new row of data and commit the changes.
# PyMySQL_insert_data.py # Copyright (c) FYIcenter.com import pymysql con = pymysql.connect(host="127.0.0.1", port=3306, \ user="guest", password="retneciyf") con.select_db("test") sql = "CREATE TABLE fyi_sites (id INTEGER PRIMARY KEY, \ url VARCHAR(80) NOT NULL, title VARCHAR(1024))" con.query(sql) sql = "INSERT INTO fyi_sites (id, url, title) \ VALUES (101, 'dev.fyicenter.com', 'Developer FYI')" con.query(sql) con.commit() con.close()
2. Run the Python script.
fyicenter> python3 PyMySQL_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_sites; +-----+-------------------+---------------+ | id | url | title | +-----+-------------------+---------------+ | 101 | dev.fyicenter.com | Developer FYI | +-----+-------------------+---------------+ 1 row in set (0.00 sec)
⇒ Cursor.execute() with PyMySQL
2021-09-09, 691🔥, 0💬
Popular Posts:
What's Wrong with "while ($c=fgetc($f)) {}" in PHP? If you are using "while ($c=fgetc($f)) {}" to lo...
How to use the "set-body" Policy Statement for an Azure API service operation? The "set-body" Policy...
Where to find tutorials on EPUB file format? I want to know how to create EPUB books. Here is a larg...
How to start Docker Daemon, "dockerd", on CentOS systems? If you have installed Docker on your CentO...
How to include additional claims in Azure AD v2.0 id_tokens? If you want to include additional claim...