# Importing the necessary library
import sqlite3
# Connecting to a SQLite database (replace 'example.db' with the path to your database file)
conn = sqlite3.connect('example.db')
# Creating a cursor object to execute queries
cursor = conn.cursor()
# Example: Creating a table in the database
create_table_query = '''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
);
'''
cursor.execute(create_table_query)
# Example: Inserting data into the table
user_data = (1, 'John Doe', 25)
insert_query = 'INSERT INTO users (id, name, age) VALUES (?, ?, ?)'
cursor.execute(insert_query, user_data)
# Example: Fetching data from the table
select_query = 'SELECT * FROM users'
cursor.execute(select_query)
rows = cursor.fetchall()
for row in rows:
print(row)
# Committing the changes and closing the connection
conn.commit()
conn.close()