Articles

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
    Syntax:
    import mysql.connector

2. Create the connection object to connect with xampp server using the below credentials.
    Syntax:
    connection_obj = mysql.connector.connect(
                          host="localhost",
                          user="root",
                          password=""
                                                                        )
3. Create cursor object to execute the query.
    Syntax:
    connection_obj.cursor()

4. Write sql query to insert records in a table.
    Syntax:
    cursor.execute(insert_query,record)
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. 
     Syntax:
      connection_obj.commit()

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 
#hostname-localhost
#username-root
#password-''(empty) and
#database is tutorialsinhand
connection_obj = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="tutorialsinhand"
)

#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
cursor_object.execute(insert_query,record)


#commit
connection_obj.commit()

print("Inserted Successfully !")

Output:

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 tutorialsinhand.com

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!