Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge 4: Update | CRUD
course content

Зміст курсу

SQL in Python Projects

Challenge 4: UpdateChallenge 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 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
    )
''')

# 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()
      

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

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

Зміст курсу

SQL in Python Projects

Challenge 4: UpdateChallenge 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 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
    )
''')

# 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()
      

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

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