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 4: Update
Complete all parts of the task in sequence
- 🔖 Use your code from Challenge 3: Creation
- 📼 Add a few more records to the
users
table - 🔄 Update email in record from the
users
table where id = 1 - 📃 Get all entries from the
users
table - 🎞 Output all records from
users
tables - 🪖 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
)
''')
# UPDATE the record
query = "UPDATE users SET email = 'new@gmail.com' WHERE id = 1"
cursor.execute(query)
# Execute SELECT query
select_query = "SELECT * FROM users"
cursor.execute(select_query)
# Output
data = cursor.fetchall()
print(data)
# Save changes and close the connection
conn.commit()
conn.close()
Everything was clear?
Section 3. Chapter 7