-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insert_tables.py
43 lines (35 loc) · 958 Bytes
/
Insert_tables.py
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
36
37
38
39
40
41
42
43
#Inserting tables
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password = "",
database = "my_database"
)
mycursor = mydb.cursor()
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
#Inserting Multiple rows
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password = "",
database = "my_database"
)
mycursor = mydb.cursor()
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
('Himani', 'Highschool para'),
('Nitya', 'Prashanti vihar'),
('Vicky', 'Amby valley'),
('Shakti', 'Ekamra Nagar'),
('Taps', 'Kalinga Nagar'),
('Chandan', 'Jaydev Vihar')
]
mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "was inserted.")