0% found this document useful (0 votes)
6 views

Python_CRUD_and_Multithreading_Notes

The document provides an overview of Python Database Connectivity using CRUD operations, detailing how to connect to MySQL and perform Create, Read, Update, and Delete actions. It also explains multithreading in Python, including its definition, advantages, and methods of implementation such as using the Thread class and ThreadPoolExecutor. Key concepts and limitations of multithreading, particularly regarding the Global Interpreter Lock (GIL), are also discussed.

Uploaded by

deependras7410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python_CRUD_and_Multithreading_Notes

The document provides an overview of Python Database Connectivity using CRUD operations, detailing how to connect to MySQL and perform Create, Read, Update, and Delete actions. It also explains multithreading in Python, including its definition, advantages, and methods of implementation such as using the Thread class and ThreadPoolExecutor. Key concepts and limitations of multithreading, particularly regarding the Global Interpreter Lock (GIL), are also discussed.

Uploaded by

deependras7410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Notes: Database Connectivity & Multithreading

Python Database Connectivity Using CRUD

What is Python Database Connectivity Using CRUD Operations

1. Introduction:

Python allows connection with databases like MySQL, SQLite, etc., to perform operations on stored data.

These operations are called CRUD:

- 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:

sql = "INSERT INTO students (name, age) VALUES (%s, %s)"

val = ("Amit", 21)

cursor.execute(sql, val)

con.commit()
Python Notes: Database Connectivity & Multithreading

Read:

cursor.execute("SELECT * FROM students")

result = cursor.fetchall()

for row in result:

print(row)

Update:

sql = "UPDATE students SET age = %s WHERE name = %s"

val = (22, "Amit")

cursor.execute(sql, val)

con.commit()

Delete:

sql = "DELETE FROM students WHERE name = %s"

val = ("Amit",)

cursor.execute(sql, val)

con.commit()

Close Connection:

cursor.close()

con.close()

Key Points:

- Use cursor to run SQL queries.

- Use commit() to save changes.

- Always close the connection.


Python Notes: Database Connectivity & Multithreading

Multithreading in Python

What is Multithreading?

1. Definition:

Multithreading is a technique where multiple threads run concurrently in a program, sharing the same

memory. It helps in performing multiple tasks at once.

2. Advantages:

- Improves performance (especially for I/O tasks)

- Makes programs more responsive

- Useful for background tasks

3. Methods of Multithreading in Python:

Method 1: Using Thread class directly

import threading

def task():

print("Hello")

t = threading.Thread(target=task)

t.start()

t.join()

Method 2: Subclassing Thread

class MyThread(threading.Thread):

def run(self):

print("Thread running")

t = MyThread()

t.start()

t.join()
Python Notes: Database Connectivity & Multithreading

Method 3: ThreadPoolExecutor

from concurrent.futures import ThreadPoolExecutor

def task(n):

print(f"Task {n} running")

with ThreadPoolExecutor(max_workers=3) as executor:

for i in range(5):

executor.submit(task, i)

4. Important Concepts:

- start(): starts thread

- join(): waits for thread to finish

- daemon: background thread

- Lock: used to handle shared data

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.

You might also like