How to get last insert ID after insert in MySQL db on Python

Python

To get last raw ID after insert row into MySQL just use cursor.lastrowid


import mysql.connector

# connect to mysql db
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database = 'mydb'
)

cursor = mydb.cursor()

# insert data into mysql
cursor.execute('INSERT INTO users (name, email) (%s, %s)', ('Jhon', '[email protected]'))
mydb.commit()

# get the last ID
print(cursor.lastrowid)