python mysql insert()

python mysql insert()

In this Python tutorial, we will see how to insert records into MySQL database in XAMPP server.


Before that make sure that you have created a database named - tutorialsinhand  and table inside it market.

See the table structure:

python mysql insert

Now we have to insert records into the market database.

In SQL, INSERT INTO statement is used to insert new records into the table.

If we want to insert records in all the columns of the table, then we don't need to specify column name.

But we should ensure that the values are inserted in same order as the columns in the table.


Syntax for INSERT in all columns 

INSERT INTO table_name
VALUES(value1, value2,...,valueN);

Let's see the steps to insert single recors into the market table.

1. Import mysql.connector module
    import mysql.connector

2. Create the connection object to connect with xampp server using the below credentials.
    connection_obj = mysql.connector.connect(
3. Create cursor object to execute the query.

4. Write sql query to insert records in a table.
SQL Query to create database:
INSERT INTO market VALUES (%s, %s,%s)
%s specifies value.

record is the tuple that holds values to be inserted.

5. Commit the database using commit() function, without committing the record is not inserted into table. 

Let's see how to insert single record in mysql through python.

Here, we are inserting a record with the following values in market table.

#import the module
import mysql.connector

#create the connection by specifying the 
#password-''(empty) and
#database is tutorialsinhand
connection_obj = mysql.connector.connect(

#creating cursor object
cursor_object = connection_obj.cursor()

#insert record
insert_query = "INSERT INTO market VALUES (%s, %s,%s)"

#record to be inserted
record = ("koti-cente","kakumanu",8)

#execute insert query


print("Inserted Successfully !")


Inserted Successfully !

Let's check in xampp server whether the record is inserted or not in market table.

open "http://localhost/phpmyadmin/" in new tab in your browser to open xampp.

python mysql insert

Python MySql

Would you like to see your article here on tutorialsinhand. Join Write4Us program by

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Sep 17,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!