How to insert value into table of mysql database using python on CentOS and RHEL 6/7 step by step guide


How to insert value into table of mysql database using python on CentOS and RHEL 6/7  step by step guide


In this article we will learn how to insert value into table using python language.
We need to install the mysql.connector module using the below command.

#pip3 install mysql.connector

Please follow the below link to create table using python language.

https://techrideradmin.blogspot.com/2019/06/how-to-create-and-show-table-in-mysql-using-python-on-centos-rhel-step-by-step-guide.html

How to insert value into table using python

First we need to create one file named insert.py and enter the below code into this file  .

import mysql.connector

 

mydb = mysql.connector.connect(

  host="localhost",

  user="root",

  passwd="abc123",

  database="pythonclass"

)

 

cur = mydb.cursor()

 

query = "INSERT INTO student (name, location) VALUES (%s, %s)"

value = ("Rahul", "Pune")

cur.execute(query, value)

 

mydb.commit()


Row will be effected after execute the mydb.commit() command.
You can enter multiple row into table using executemany() method.

How to execute the python progam

To execute the python program we need to execute the below command.

[root@localhost ~]# python3 insert.py

After executing the above command record has been inserted into the table.

Now enter the mysql promt and verify it is inserted or not using “select” statement.


mysql> select * from student;

mysql> select * from student;
+-------+----------+
| name  | location |
+-------+----------+
| Rahul | Pune     |
+-------+----------+
1 row in set (0.00 sec)
How to insert value into table of mysql database using python on CentOS and RHEL 6/7  step by step guide
How to insert value into table of mysql database using python on CentOS and RHEL 6/7  step by step guide


That’s all. If this article is helpful please share it!!!!




Please Donate To Bitcoin Address: [[address]]

Donation of [[value]] BTC Received. Thank You.
[[error]]
SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

1 comments:

  1. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles Python Programming Training

    ReplyDelete