Added data connection examples to Python sheet.
This commit is contained in:
parent
b5790400c5
commit
f2f001fb88
129
python.md
129
python.md
|
|
@ -15,6 +15,17 @@
|
|||
11. [File Handling](#file-handling)
|
||||
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.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
|
||||
|
||||
|
|
@ -214,5 +225,123 @@ obj = MyClass("John")
|
|||
obj.say_hello() # Outputs: Hello, John!
|
||||
```
|
||||
|
||||
## Working with Data
|
||||
|
||||
```python
|
||||
# Import necessary modules
|
||||
import sqlite3
|
||||
import json
|
||||
```
|
||||
|
||||
## 14. SQLite3 Database Connection
|
||||
|
||||
### 14.1 Connecting to a Database
|
||||
|
||||
```python
|
||||
# Connect to a database (creates a new database if it doesn't exist)
|
||||
conn = sqlite3.connect('example.db')
|
||||
|
||||
# Create a cursor object to execute SQL commands
|
||||
cursor = conn.cursor()
|
||||
```
|
||||
|
||||
### 14.2 Creating a Table
|
||||
|
||||
```python
|
||||
# Create a table
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
username TEXT NOT NULL,
|
||||
email TEXT NOT NULL
|
||||
)
|
||||
''')
|
||||
|
||||
# Commit the changes
|
||||
conn.commit()
|
||||
```
|
||||
|
||||
### 14.3 Inserting Data
|
||||
|
||||
```python
|
||||
# Insert data into the table
|
||||
cursor.execute('''
|
||||
INSERT INTO users (username, email)
|
||||
VALUES ('john_doe', 'john@example.com')
|
||||
''')
|
||||
|
||||
# Commit the changes
|
||||
conn.commit()
|
||||
```
|
||||
|
||||
### 14.4 Querying Data
|
||||
|
||||
```python
|
||||
# Fetch all rows from the table
|
||||
cursor.execute('SELECT * FROM users')
|
||||
rows = cursor.fetchall()
|
||||
|
||||
# Print the results
|
||||
for row in rows:
|
||||
print(row)
|
||||
```
|
||||
|
||||
### 14.5 Updating Data
|
||||
|
||||
```python
|
||||
# Update data in the table
|
||||
cursor.execute('''
|
||||
UPDATE users
|
||||
SET email = 'john.doe@example.com'
|
||||
WHERE username = 'john_doe'
|
||||
''')
|
||||
|
||||
# Commit the changes
|
||||
conn.commit()
|
||||
```
|
||||
|
||||
### 14.6 Deleting Data
|
||||
|
||||
```python
|
||||
# Delete data from the table
|
||||
cursor.execute('''
|
||||
DELETE FROM users
|
||||
WHERE username = 'john_doe'
|
||||
''')
|
||||
|
||||
# Commit the changes
|
||||
conn.commit()
|
||||
```
|
||||
|
||||
### 14.7 Closing the Connection
|
||||
|
||||
```python
|
||||
# Close the cursor and connection when done
|
||||
cursor.close()
|
||||
conn.close()
|
||||
```
|
||||
|
||||
## 15. JSON Data Connection
|
||||
|
||||
### 15.1 Loading JSON Data
|
||||
|
||||
```python
|
||||
# Load JSON data from a file
|
||||
with open('data.json', 'r') as json_file:
|
||||
data = json.load(json_file)
|
||||
print(data)
|
||||
```
|
||||
|
||||
### 15.2 Writing JSON Data
|
||||
|
||||
```python
|
||||
# Write Python data to a JSON file
|
||||
data_to_write = {'name': 'John', 'age': 30, 'city': 'New York'}
|
||||
|
||||
with open('output.json', 'w') as json_file:
|
||||
json.dump(data_to_write, json_file)
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue