Contenido del Curso
SQL in Python Projects
1. Development Environment on Your PC
2. Introduction to SQLite
4. More About SQLite
SQL in Python Projects
Challenge 5: Delete
Complete all parts of the task in sequence
- 🔖 Use your code from Challenge 3: Creation;
- 📼 Add a few more records to the
users
table; - 🗑 Delete record from the
users
table where id = 1; - 📃 Get all records from the
users
table; - 🎞 Output all records from
users
tables .
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
)
''')
# DELETE the record
query = "DELETE FROM users 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()
¿Todo estuvo claro?
Sección 3. Capítulo 9
Contenido del Curso
SQL in Python Projects
1. Development Environment on Your PC
2. Introduction to SQLite
4. More About SQLite
SQL in Python Projects
Challenge 5: Delete
Complete all parts of the task in sequence
- 🔖 Use your code from Challenge 3: Creation;
- 📼 Add a few more records to the
users
table; - 🗑 Delete record from the
users
table where id = 1; - 📃 Get all records from the
users
table; - 🎞 Output all records from
users
tables .
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
)
''')
# DELETE the record
query = "DELETE FROM users 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()
¿Todo estuvo claro?
Sección 3. Capítulo 9