-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_insert.py
More file actions
35 lines (28 loc) · 874 Bytes
/
data_insert.py
File metadata and controls
35 lines (28 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Python implementation to insert data into a table in MySQL
import mysql.connector
# connecting to the mysql server
db = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="employee_db"
)
# cursor object c
c = db.cursor()
# insert statement for tblemployee
# this statement will enable us to insert multiple rows at once.
employeetbl_insert = """INSERT INTO tblemployee (
empname,
department,
salary)
VALUES (%s, %s, %s)"""
# we save all the row data to be inserted in a data variable
data = [("Vani", "HR", "100000"),
("Krish", "Accounts", "60000"),
("Aishwarya", "Sales", "25000"),
("Govind", "Marketing", "40000")]
# execute the insert commands for all rows and commit to the database
c.executemany(employeetbl_insert, data)
db.commit()
# finally closing the database connection
db.close()