Online SQLite3 Compiler

Create a and use a SQLite database

Python
import sqlite3
import os

# Create a new SQLite database
db_path = 'example.db'
conn = sqlite3.connect(db_path)
cursor = conn.cursor()

# Create a table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    age INTEGER
)
''')

# Insert some data
users = [
    ('Alice', 30),
    ('Bob', 25),
    ('Charlie', 35)
]
cursor.executemany('INSERT INTO users (name, age) VALUES (?, ?)', users)

# Commit changes and close connection
conn.commit()
conn.close()

# Reopen connection and query data
conn = sqlite3.connect(db_path)
cursor = conn.cursor()

# Perform a simple query
cursor.execute('SELECT * FROM users')
results = cursor.fetchall()

# Print results
for row in results:
    print(f"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}")

# Close connection
conn.close()
Click Run or press shift + ENTER to run code.