Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge 3: Read | CRUD
SQL in Python Projects

Challenge 3: Read 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 It is how code looks like text again
3. 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()

Все було зрозуміло?

Секція 3. Розділ 5
course content

Зміст курсу

SQL in Python Projects

Challenge 3: Read 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 It is how code looks like text again
3. 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()

Все було зрозуміло?

Секція 3. Розділ 5
some-alt