
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How do I get the id after INSERT into MySQL database in 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.

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 −

The following is the output.

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

- Related Articles
- How to get the id after INSERT into MySQL database using Python?
- How do I insert a record from one Mongo database into another?
- How to insert DECIMAL into MySQL database?
- How do we insert/store a file into MySQL database using JDBC?
- How to insert data into a MySQL database with Java?
- Get the new record key ID from MySQL insert query?
- How do I insert a JPEG image into a Python Tkinter window?
- How to get username using ID from another table in MySQL database?
- How do I remove a MySQL database?
- How do I insert a special character such as ' (single quote) into MySQL?
- How do I insert a NULL value in MySQL?
- How to insert a Python tuple in a MySQL database?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- Java application to insert null value into a MySQL database?
- How do I get the resource id of an image if I know its name in Android?

Advertisements