Get the id after INSERT into MySQL database using Python Last Updated : 11 Dec, 2020 Comments Improve Suggest changes Like Article Like Report Prerequisites: MySQL, mysql-connector for python The task here is to draft a Python program that works with SQL support to connect data. Whenever insertion into the database takes place, the ID of the row inserted will be printed. To connect python with the database we are using MySQL connector. The work of mysql-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and MySQL Server. INSERT Clause in SQL As the name suggests, it inserts data into the database. Certain rules need to be followed while using the insert command. The data to be updated should either be in the same order as the columns of the database or names of the columns should be given to the query along with the data to be inserted. Syntax: INSERT INTO <tablename>(Col1,Col2, ...) VALUES(....); To print the ID of the last inserted row lastrowid will be used. This is a special keyword that prints the ID of the row last inserted but to do so there are certain protocols that had to be kept in mind before employing this: ID column of the database should be a primary keyID column should auto-incremented. Given below is the implementation of the same: Database In Use: Below is the implementation: Python3 import mysql.connector mydb = mysql.connector.connect( host = 'localhost', database = 'employee', user = 'root', password = 'Your_pass' ) cs = mydb.cursor() statement = "INSERT INTO geekstudent( id, name,gender, subject)\ VALUES(6,'Shoit','M', 'ML')" cs.execute(statement) mydb.commit() print(cs.rowcount, " record(s) added") print(cs.lastrowid) Output: 1 record(s) added 0 Updated Database Output: Comment More infoAdvertise with us Next Article Get the id after INSERT into MySQL database using Python V vanshikagoyal43 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 Python-mySQL Practice Tags : python Similar Reads Create Database in MariaDB using PyMySQL in Python MariaDB is an open source Database Management System and its predecessor to MySQL. The pymysql client can be used to interact with MariaDB similar to that of MySQL using Python. In this article we will look into the process of creating a database using pymysql. To create a database use the below syn 2 min read Create MySQL Database Login Page in Python using Tkinter Prerequisites: Python GUI â tkinter, Python MySQL â Select QueryTkinter is one of the Python libraries which contains many functions for the development of graphic user interface pages and windows. Login pages are important for the development of any kind of mobile or web application. This page is m 2 min read Inserting data into a new column of an already existing table in MySQL using Python Prerequisite: Python: MySQL Create Table In this article, we are going to see how to Inserting data into a new column of an already existing table in MySQL using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a 2 min read Python MariaDB - Insert into Table using PyMySQL MariaDB is an open source Database Management System and its predecessor to MySQL. The pymysql client can be used to interact with MariaDB similar to that of MySQL using Python. In this article we will look into the process of inserting rows to a table of the database using pymysql. You can insert o 2 min read How to Insert Dummy Data into Databases using Flask Dummy data addition is a crucial component of development, particularly when you are just building up your project. Although adding dummy data to your database might be highly annoying, doing so is absolutely necessary to speed up your development process. The sample code for Flask to insert dummy d 4 min read Like