Menu Close

Python MySQL Insert Into Table

In this Python MySQL insert into table tutorial, we are going to learn all about how to insert new records into the table after created of the table in MySQL.
In the previous tutorial, we have seen the complete process to create a table in MySQL using Python.

Here in this guide, we will see a complete process to insert single rows into the table as well as multiple rows into the table using Python MySQL connector.

Make sure you have already installed MySQL connector in your machine using pip as well as create a database and table.

Python MySQL Insert Into Table:

As we know that, MySQL provide “INSERT INTO” to insert new record into table in MySQL database.

Example:

Here we will insert single record into the table named “students”.

import mysql.connector
mydb = mysql.connector.connect(

	host="localhost",
	user="root",
	password="root21",
	database = "demodb",
    port = 3308
	
)

cursor = mydb.cursor()
cursor.execute('INSERT INTO students (id, name, age) VALUES(1, "Vishvajit", 12)')
mydb.commit()
print(mycursor.rowcount, "was inserted.")

Output:

1 record was inserted.

Insert Multiple Row:

Here we will insert multiple records into the table named “students“. To insert multiple records into the database table, use executemany() methods.

Example:

import mysql.connector
mydb = mysql.connector.connect(

	host="localhost",
	user="root",
	password="root21",
	database = "demodb",
    port = 3308
	
)

cursor = mydb.cursor()
sql = 'INSERT INTO students (id, name, age) VALUES(%s, %s, %s)'
vals = [
(2, "John", 22),
(3, "Alex", 32),
(4, "Peter", 12),
(5, "Alex Peter", 12),
(6, "Alsena", 45),
(7, "Jaini", 56)
]

cursor.executemany(sql, vals)
mydb.commit()
print(mycursor.rowcount, "was inserted.")

Output:

6 record was inserted.

Note:- Above credentials may be different according to your MySQL configuration.
I am showing all the credentials for only testing purposes, It will not working in your case.

Check Inserted Record exist:

To check the records inserted into the database table or not, MySQL provide “SELECT” statement.

Example:

import mysql.connector
mydb = mysql.connector.connect(

	host="localhost",
	user="root",
	password="root21",
	database="demodb",
    port = 3308
	
)

cursor = mydb.cursor()
cursor.execute("SELECT * from students")
for record in cursor:
	print(record)

Output:

(1, 'Vishvajit', 12)
(2, 'John', 22)
(3, 'Alex', 32)
(4, 'Peter', 12)
(5, 'Alex Peter', 12)
(6, 'Alsena', 45)
(7, 'Jaini', 56)

Conclusion

In this Python MySQL Insert Into Table tutorial, you have learned all about how to insert single and multiple records into the table in the database.
Python MySQL connector is the best for connect Python applications to the MySQL database and execute SQL queries. After created of database successfully, you can perform query on that particular database.

If this article for you, please share and keep visiting for further Python MySQL database tutorials.

For more information about Python MySQL Insert :- Click Here

More Python MySQL Tutorials

Previous Article
Next Article

Was this post helpful?

Related Posts