Yêu cầu: MySQL 8.0.12, Python 3.6.3+, thư viện pymysql (pip install pymysql).
Dữ liệu mẫu (bảng student, database business)
mysql> SELECT * FROM student;
+------+-------+ | id | Name | +------+-------+ | 1 | John | | 2 | Bob | | 3 | Smith | +------+-------+ 3 rows in set (0.00 sec)
Code Python (pymysql)




Cốt lõi: Sau cursor.execute(INSERT...), dùng cursor.lastrowid để lấy ID auto_increment vừa chèn.