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

Notes on File Handling

The document provides an overview of file handling in Python, including file modes, methods for opening, reading, writing, and closing files, as well as best practices using the 'with' statement. It also covers Python's connectivity with SQL databases using the mysql.connector library, detailing steps to establish a connection, execute SQL queries, and perform common operations like insert, update, and delete. Additionally, it outlines key SQL concepts, commands, and joins, aimed at aiding revision and practice in these areas.

Uploaded by

palash kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Notes on File Handling

The document provides an overview of file handling in Python, including file modes, methods for opening, reading, writing, and closing files, as well as best practices using the 'with' statement. It also covers Python's connectivity with SQL databases using the mysql.connector library, detailing steps to establish a connection, execute SQL queries, and perform common operations like insert, update, and delete. Additionally, it outlines key SQL concepts, commands, and joins, aimed at aiding revision and practice in these areas.

Uploaded by

palash kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Notes on File Handling, Connectivity, and SQL

File Handling in Python

Basics of File Handling

File handling allows us to create, read, write, and delete files using Python. It provides a
way to persist data.

File Modes

• 'r': Read mode (default).

• 'w': Write mode (overwrites existing content).

• 'x': Create mode (fails if the file exists).

• 'a': Append mode.

• 'b': Binary mode.

• 't': Text mode (default).

File Handling Methods

1. Opening Files:

file = open('example.txt', 'r')

2. Reading Files:

3. content = file.read()

print(content)

4. Writing Files:

5. file = open('example.txt', 'w')

file.write('Hello, World!')

6. Closing Files:

file.close()

7. Using with Statement (recommended):

8. with open('example.txt', 'r') as file:

content = file.read()

Examples
• Writing to a File:

• with open('data.txt', 'w') as file:

file.write('Python File Handling Example')

• Reading Line by Line:

• with open('data.txt', 'r') as file:

• for line in file:

print(line)

Connectivity (Python with SQL)

Python Database Connectivity

Python provides the mysql.connector library to connect and interact with SQL
databases.

Steps to Connect Python with SQL

1. Import the Connector:

import mysql.connector

2. Establish a Connection:

3. conn = mysql.connector.connect(

4. host='localhost',

5. user='root',

6. password='password',

7. database='school'

8. Create a Cursor Object:

cursor = conn.cursor()

9. Execute SQL Queries:

10. cursor.execute("SELECT * FROM students")

11. result = cursor.fetchall()

12. for row in result:


print(row)

13. Close the Connection:

conn.close()

Common SQL Operations in Python

• Insert Data:

• cursor.execute("INSERT INTO students (id, name) VALUES (1, 'John')")

conn.commit()

• Update Data:

• cursor.execute("UPDATE students SET name='Jane' WHERE id=1")

conn.commit()

• Delete Data:

• cursor.execute("DELETE FROM students WHERE id=1")

conn.commit()

SQL Concepts

Relational Databases

• A Relational Database organizes data into tables consisting of rows and


columns.

SQL Commands

1. DDL (Data Definition Language): CREATE, ALTER, DROP.

2. DML (Data Manipulation Language): INSERT, UPDATE, DELETE.

3. DQL (Data Query Language): SELECT.

4. DCL (Data Control Language): GRANT, REVOKE.

Common SQL Queries

• Create Table:

• CREATE TABLE students (

• id INT PRIMARY KEY,

• name VARCHAR(50),
• age INT

);

• Insert Data:

INSERT INTO students (id, name, age) VALUES (1, 'Alice', 20);

• Select Data:

SELECT * FROM students;

• Update Data:

UPDATE students SET age = 21 WHERE id = 1;

• Delete Data:

DELETE FROM students WHERE id = 1;

Joins in SQL

• INNER JOIN: Combines rows with matching values in both tables.

• SELECT students.name, courses.course_name

• FROM students

INNER JOIN courses ON students.id = courses.student_id;

• 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!

You might also like