Modified Python Sheet.

This commit is contained in:
CSnap 2023-11-19 08:04:22 +00:00
parent b163490cea
commit 615bc8c2e7
1 changed files with 20 additions and 30 deletions

View File

@ -16,25 +16,15 @@
12. [Exception Handling](#exception-handling)
13. [Classes and Objects](#classes-and-objects)
14. [SQLite3 Database Connection](#sqlite3-database-connection)
14.1 [Connecting to a Database](##connecting-to-a-database)
14.1 [Connecting to a Database](#connecting-to-a-database)
14.2 [Creating a Table](#creating-a-table)
14.3 [Inserting Data](#inserting-data)
14.4 [Querying Data](#querying-data)
14.5 [Updating Data](#updating-data)
14.6 [Deleting Data](#deleting-data)
14.7 [Closing the Connection](#closing-the-connection)
15. [JSON Data Connection](#json-data-connection)
15.1 [Loading JSON Data](#loading-json-data)
15.2 [Writing JSON Data](#writing-json-data)
## Variables
@ -243,9 +233,9 @@ import sqlite3
import json
```
## 14. SQLite3 Database Connection
## SQLite3 Database Connection
### 14.1 Connecting to a Database
### Connecting to a Database
```python
# Connect to a database (creates a new database if it doesn't exist)
@ -255,7 +245,7 @@ conn = sqlite3.connect('example.db')
cursor = conn.cursor()
```
### 14.2 Creating a Table
### Creating a Table
```python
# Create a table
@ -271,7 +261,7 @@ cursor.execute('''
conn.commit()
```
### 14.3 Inserting Data
### Inserting Data
```python
# Insert data into the table
@ -284,7 +274,7 @@ cursor.execute('''
conn.commit()
```
### 14.4 Querying Data
### Querying Data
```python
# Fetch all rows from the table
@ -296,7 +286,7 @@ for row in rows:
print(row)
```
### 14.5 Updating Data
### Updating Data
```python
# Update data in the table
@ -310,7 +300,7 @@ cursor.execute('''
conn.commit()
```
### 14.6 Deleting Data
### Deleting Data
```python
# Delete data from the table
@ -323,7 +313,7 @@ cursor.execute('''
conn.commit()
```
### 14.7 Closing the Connection
### Closing the Connection
```python
# Close the cursor and connection when done
@ -331,9 +321,9 @@ cursor.close()
conn.close()
```
## 15. JSON Data Connection
## JSON Data Connection
### 15.1 Loading JSON Data
### Loading JSON Data
```python
# Load JSON data from a file
@ -342,7 +332,7 @@ with open('data.json', 'r') as json_file:
print(data)
```
### 15.2 Writing JSON Data
### Writing JSON Data
```python
# Write Python data to a JSON file