Course Content
SQL in Python Projects
1. Development environment on your PC
2. Introduction to SQLite
4. More about SQLite
SQL in Python Projects
Challenge 3: Read
Complete all parts of the task in sequence
- 🔖 Use your code from Challenge 2: Creation
- 📼 Add a few more records to the
users
table - 📃 Get all entries from the users table where
user_name = 'Alex'
- 🎞 Output the received records
- 🪖 Save the changes
- 🔑 Close the database connection
1. It is recommended to do ...
2. Some text
3. Use such method.
2. Some text
It is how code looks like
text again3. Use such method.
import sqlite3
# Connect to the database or create it
conn = sqlite3.connect('mydatabase.db')
# Create a cursor object
cursor = conn.cursor()
# Create the "users" table with fields
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER,
user_name TEXT,
email TEXT,
password TEXT
)
''')
query = "INSERT INTO users (id, user_name, email, password) VALUES (?, ?, ?, ?)"
# Insert a new record
user_data = (1, 'Alex', 'AlexMain@gmail.com', 'ZXCV2000')
cursor.execute(query, user_data)
# SELECT Alex query
select_alex_query = "SELECT * FROM users WHERE user_name = 'Alex'"
# Execute SELECT Alex query
cursor.execute(select_alex_query)
data = cursor.fetchall()
# Output
print(data)
# Save changes and close the connection
conn.commit()
conn.close()
Everything was clear?
Section 3. Chapter 5