How do I get the id after INSERT into MySQL database in Python?

MySQLMySQLi Python

First, we need to complete the below requirements. The following are the prerequisites.

  • We need to install MySQL version 8.0.12.
  • We need to install Python version 3.6.3 (32 bit).
  • We need to install pymysql with the help of pip command.

To install “pymysql” with the help of pip command.

pymysql

The database name is ‘business’ and the table name is ‘student’ with ID 3 after inserting records.

Here is the query.

mysql> select *from student;

The following is the output.

+------+-------+
| id   | Name  |
+------+-------+
|    1 | John  |
|    2 | Bob   |
|    3 | Smith |
+------+-------+
3 rows in set (0.00 sec)

We can get it with the help of Python for the same table name and database business.

The Python code snapshot is as follows −

Python Code

The following is the output.

Python Output

The output for MySQL and Python gets the ID after inserting::

ID after inserting
raja
Published on 23-Nov-2018 17:02:34
Advertisements