PYTHON Tutorial
A database is a structured collection of data that is organized for efficient access, management, and storage. Its purpose is to store and retrieve data in a systematic manner.
Structured Query Language (SQL) is the standard language used to interact with databases. It allows you to:
Relational databases organize data into multiple related tables. They utilize keys to establish relationships between tables, ensuring data integrity and consistency.
CREATE DATABASE my_database;
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT);
INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]');
SELECT * FROM users;
UPDATE users SET name = 'John Smith' WHERE id = 1;
DELETE FROM users WHERE id = 1;
import sqlite3
# Connect to the database
conn = sqlite3.connect('my_database.db')
# Create a cursor
c = conn.cursor()
# Create a table
c.execute('''CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)''')
# Insert data
c.execute('''INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]')''')
# Commit the changes
conn.commit()
# Retrieve data
for row in c.execute('SELECT * FROM users'):
print(row)
# Close the connection
conn.close()