Python_CRUD_and_Multithreading_Notes
Python_CRUD_and_Multithreading_Notes
1. Introduction:
Python allows connection with databases like MySQL, SQLite, etc., to perform operations on stored data.
- C - Create (Insert)
- R - Read (Select)
- U - Update
- D - Delete
2. Connecting to MySQL:
import mysql.connector
con = mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword",
database="yourdbname"
cursor = con.cursor()
3. CRUD Operations:
Create:
cursor.execute(sql, val)
con.commit()
Python Notes: Database Connectivity & Multithreading
Read:
result = cursor.fetchall()
print(row)
Update:
cursor.execute(sql, val)
con.commit()
Delete:
val = ("Amit",)
cursor.execute(sql, val)
con.commit()
Close Connection:
cursor.close()
con.close()
Key Points:
Multithreading in Python
What is Multithreading?
1. Definition:
Multithreading is a technique where multiple threads run concurrently in a program, sharing the same
2. Advantages:
import threading
def task():
print("Hello")
t = threading.Thread(target=task)
t.start()
t.join()
class MyThread(threading.Thread):
def run(self):
print("Thread running")
t = MyThread()
t.start()
t.join()
Python Notes: Database Connectivity & Multithreading
Method 3: ThreadPoolExecutor
def task(n):
for i in range(5):
executor.submit(task, i)
4. Important Concepts:
5. Limitation:
Due to GIL, only one thread runs at a time in Python interpreter. For CPU-bound tasks, multiprocessing is
better.
Conclusion:
Multithreading is great for I/O-bound and background tasks and is implemented using the threading module
or concurrent.futures.