Python/sqlite3/basics/test.py
import sqlite3
import os
# Construct the relative path to the database file
db_path = os.path.join(os.path.dirname(__file__), 'sql.db')
# Connect to the database using the constructed path
conn = sqlite3.connect(db_path)
# Create a cursor object
cursor = conn.cursor()
# Create a table
cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)''')
# Insert data into the table
cursor.execute("INSERT INTO users (name, age) VALUES ('Alice', 30)")
cursor.execute("INSERT INTO users (name, age) VALUES ('Bob', 25)")
# Commit the transaction
conn.commit()
# Query the database
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
for row in rows:
print(row)
# Close the connection
conn.close()