|
| 1 | +### **Day 23: Database Connection** |
| 2 | +Python can connect to various types of databases using different database libraries. One common library for working with databases in Python is `sqlite3` for SQLite databases. |
| 3 | + |
| 4 | +- **Connecting to a Database:** |
| 5 | + - Use the `sqlite3.connect()` method to connect to a database. |
| 6 | + - This method creates a new database if it doesn't exist or opens an existing one. |
| 7 | + |
| 8 | +**Example of connecting to an SQLite database:** |
| 9 | +```python |
| 10 | +import sqlite3 |
| 11 | + |
| 12 | +# Connect to a database (creates a new one if it doesn't exist) |
| 13 | +conn = sqlite3.connect("my_database.db") |
| 14 | + |
| 15 | +# Create a cursor object to execute SQL commands |
| 16 | +cursor = conn.cursor() |
| 17 | + |
| 18 | +# Close the connection when done |
| 19 | +conn.close() |
| 20 | +``` |
| 21 | + |
| 22 | +### **Day 24: Creating Tables and Inserting Data** |
| 23 | +You can create tables in a database and insert data into them using SQL commands. |
| 24 | + |
| 25 | +- **Creating Tables:** |
| 26 | + - Use the `execute()` method to run SQL CREATE TABLE queries. |
| 27 | +- **Inserting Data:** |
| 28 | + - Use the `execute()` method to run SQL INSERT queries. |
| 29 | + |
| 30 | +**Example of creating a table and inserting data:** |
| 31 | +```python |
| 32 | +import sqlite3 |
| 33 | + |
| 34 | +conn = sqlite3.connect("my_database.db") |
| 35 | +cursor = conn.cursor() |
| 36 | + |
| 37 | +# Create a table |
| 38 | +cursor.execute(""" |
| 39 | + CREATE TABLE IF NOT EXISTS students ( |
| 40 | + id INTEGER PRIMARY KEY, |
| 41 | + name TEXT, |
| 42 | + age INTEGER |
| 43 | + ) |
| 44 | +""") |
| 45 | + |
| 46 | +# Insert data into the table |
| 47 | +cursor.execute("INSERT INTO students (name, age) VALUES (?, ?)", ("Alice", 25)) |
| 48 | +cursor.execute("INSERT INTO students (name, age) VALUES (?, ?)", ("Bob", 30)) |
| 49 | + |
| 50 | +# Commit the changes and close the connection |
| 51 | +conn.commit() |
| 52 | +conn.close() |
| 53 | +``` |
| 54 | + |
| 55 | +### **Day 25: Querying Data** |
| 56 | +You can retrieve data from a database table using SQL SELECT queries. |
| 57 | + |
| 58 | +- **Querying Data:** |
| 59 | + - Use the `execute()` method to run SQL SELECT queries. |
| 60 | + - Use the `fetchall()` method to retrieve all rows from a query. |
| 61 | + |
| 62 | +**Example of querying data from a table:** |
| 63 | +```python |
| 64 | +import sqlite3 |
| 65 | + |
| 66 | +conn = sqlite3.connect("my_database.db") |
| 67 | +cursor = conn.cursor() |
| 68 | + |
| 69 | +# Query data from the table |
| 70 | +cursor.execute("SELECT * FROM students") |
| 71 | +students = cursor.fetchall() |
| 72 | + |
| 73 | +# Print the results |
| 74 | +for student in students: |
| 75 | + print(f"ID: {student[0]}, Name: {student[1]}, Age: {student[2]}") |
| 76 | + |
| 77 | +conn.close() |
| 78 | +``` |
| 79 | + |
| 80 | +### **Day 26: Updating and Deleting Data** |
| 81 | +You can use SQL UPDATE and DELETE queries to modify or remove data from a database table. |
| 82 | + |
| 83 | +- **Updating Data:** |
| 84 | + - Use SQL UPDATE queries to modify existing data. |
| 85 | +- **Deleting Data:** |
| 86 | + - Use SQL DELETE queries to remove data from the table. |
| 87 | + |
| 88 | +**Example of updating and deleting data:** |
| 89 | +```python |
| 90 | +import sqlite3 |
| 91 | + |
| 92 | +conn = sqlite3.connect("my_database.db") |
| 93 | +cursor = conn.cursor() |
| 94 | + |
| 95 | +# Update data |
| 96 | +cursor.execute("UPDATE students SET age = 26 WHERE name = 'Alice'") |
| 97 | + |
| 98 | +# Delete data |
| 99 | +cursor.execute("DELETE FROM students WHERE name = 'Bob'") |
| 100 | + |
| 101 | +# Commit the changes and close the connection |
| 102 | +conn.commit() |
| 103 | +conn.close() |
| 104 | +``` |
| 105 | + |
| 106 | +Understanding how to work with databases and SQL in Python is crucial for many real-world applications where data storage and retrieval are required. The `sqlite3` library is suitable for learning and small-scale projects, but for larger databases, you may consider other database systems like MySQL or PostgreSQL. Practice with these examples to become proficient in database operations in Python. |
0 commit comments