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