165. Working with sqlite3 Database
import sqlite3
# Connect to the database (creates the file if it doesn't exist)
connection = sqlite3.connect("example.db")
# Print success message
print("Database connected successfully!")
# Close the connection
connection.close()import sqlite3
# Connect to the database
connection = sqlite3.connect("example.db")
cursor = connection.cursor()
# Create a table
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
)
""")
print("Table created successfully!")
# Commit changes and close the connection
connection.commit()
connection.close()Last updated