Skip to content

Commit f912493

Browse files
committed
Python Mysql operation
0 parents  commit f912493

8 files changed

+132
-0
lines changed

connect.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import mysql.connector
2+
3+
db = mysql.connector.connect(
4+
host="127.0.0.1",
5+
user="root",
6+
password="123456"
7+
)
8+
9+
if db.is_connected():
10+
print("Database Connected")

create_db.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import mysql.connector
2+
3+
db = mysql.connector.connect(
4+
host="127.0.0.1",
5+
user="root",
6+
password="123456"
7+
)
8+
9+
cursor = db.cursor()
10+
cursor.execute("CREATE DATABASE employee_data")
11+
12+
print("Database Created Successful !!!")

create_table.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import mysql.connector
2+
3+
db = mysql.connector.connect(
4+
host="127.0.0.1",
5+
user="root",
6+
password="123456",
7+
database="employee_data",
8+
)
9+
10+
cursor = db.cursor()
11+
sql = """CREATE TABLE customers (
12+
customer_id INT AUTO_INCREMENT PRIMARY KEY,
13+
name VARCHAR(255),
14+
address Varchar(255)
15+
)
16+
"""
17+
cursor.execute(sql)
18+
19+
print("Table Created Successful !!!")

delete.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import mysql.connector
2+
3+
db = mysql.connector.connect(
4+
host="127.0.0.1",
5+
user="root",
6+
password="123456",
7+
database="employee_data",
8+
)
9+
10+
cursor = db.cursor()
11+
sql = "DELETE FROM customers WHERE customer_id=%s"
12+
val = (4, )
13+
cursor.execute(sql, val)
14+
15+
db.commit()
16+
17+
print("{} data deleted".format(cursor.rowcount))

insert_many.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import mysql.connector
2+
3+
db = mysql.connector.connect(
4+
host="127.0.0.1",
5+
user="root",
6+
password="123456",
7+
database="employee_data",
8+
)
9+
10+
cursor = db.cursor()
11+
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
12+
values = [
13+
("Shakib", "Magura"),
14+
("Tamin", "Ctg"),
15+
("Taskin", "Dhaka"),
16+
("Mushfiq", "Rajshahi")
17+
]
18+
19+
for val in values:
20+
cursor.execute(sql, val)
21+
22+
db.commit()
23+
24+
print("{} data added".format(cursor.rowcount))

insert_one.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import mysql.connector
2+
3+
db = mysql.connector.connect(
4+
host="127.0.0.1",
5+
user="root",
6+
password="123456",
7+
database="employee_data",
8+
)
9+
10+
cursor = db.cursor()
11+
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
12+
val = ("Mr Taif", "Dhaka")
13+
cursor.execute(sql, val)
14+
15+
db.commit()
16+
17+
print("{} data added".format(cursor.rowcount))

select.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import mysql.connector
2+
3+
db = mysql.connector.connect(
4+
host="127.0.0.1",
5+
user="root",
6+
password="123456",
7+
database="employee_data",
8+
)
9+
10+
cursor = db.cursor()
11+
sql = "SELECT * FROM customers"
12+
cursor.execute(sql)
13+
14+
results = cursor.fetchall()
15+
16+
for data in results:
17+
print(data)

select_one.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import mysql.connector
2+
3+
db = mysql.connector.connect(
4+
host="127.0.0.1",
5+
user="root",
6+
password="123456",
7+
database="employee_data",
8+
)
9+
10+
cursor = db.cursor()
11+
sql = "SELECT * FROM customers"
12+
cursor.execute(sql)
13+
14+
result = cursor.fetchone()
15+
16+
print(result)

0 commit comments

Comments
 (0)