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 2: Creation
Complete all parts of the task in sequence
- 📁 Create a new project in PyCharm.
- 📝 Create a new python file.
- 📚 Import the sqlite3 library.
- 🗄 Connect or create to the database.
- 🤚 Create a cursor.
- 📄 Create a table named
users
with the following fields:
- 📼 Insert a new record (with values from example below) into the
users
table
- 🪖 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)
# Save changes and close the connection
conn.commit()
conn.close()
Everything was clear?
Section 3. Chapter 3