Notes on File Handling
Notes on File Handling
File handling allows us to create, read, write, and delete files using Python. It provides a
way to persist data.
File Modes
1. Opening Files:
2. Reading Files:
3. content = file.read()
print(content)
4. Writing Files:
file.write('Hello, World!')
6. Closing Files:
file.close()
content = file.read()
Examples
• Writing to a File:
print(line)
Python provides the mysql.connector library to connect and interact with SQL
databases.
import mysql.connector
2. Establish a Connection:
3. conn = mysql.connector.connect(
4. host='localhost',
5. user='root',
6. password='password',
7. database='school'
cursor = conn.cursor()
conn.close()
• Insert Data:
conn.commit()
• Update Data:
conn.commit()
• Delete Data:
conn.commit()
SQL Concepts
Relational Databases
SQL Commands
• Create Table:
• name VARCHAR(50),
• age INT
);
• Insert Data:
INSERT INTO students (id, name, age) VALUES (1, 'Alice', 20);
• Select Data:
• Update Data:
• Delete Data:
Joins in SQL
• FROM students
• LEFT JOIN: Returns all rows from the left table and matching rows from the right
table.
• RIGHT JOIN: Returns all rows from the right table and matching rows from the
left table.
• FULL JOIN: Combines rows from both tables, showing all matches and
unmatched rows.
These notes should help you revise and practice file handling, Python-SQL connectivity,
and SQL concepts effectively!