[go: up one dir, main page]

0% found this document useful (0 votes)
9 views1 page

PRACTICAL

C.S PRACTICAL CLASS 12

Uploaded by

wa298825
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

PRACTICAL

C.S PRACTICAL CLASS 12

Uploaded by

wa298825
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import sqlite3

# Connect to a SQLite database (or create it if it doesn't exist)


conn = sqlite3.connect('example.db')
cursor = conn.cursor()

# Step 1: Create a table


cursor.execute('''
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER,
department TEXT
)
''')
print("Table created successfully.")

# Step 2: Insert some data


cursor.execute("INSERT INTO employees (name, age, department) VALUES ('Alice', 30,
'HR')")
cursor.execute("INSERT INTO employees (name, age, department) VALUES ('Bob', 25,
'IT')")
conn.commit()
print("Data inserted successfully.")

# Step 3: Use DELETE to remove a record


cursor.execute("DELETE FROM employees WHERE name = 'Alice'")
conn.commit()
print("Record deleted successfully.")

# Step 4: Use ALTER to modify the table structure


try:
cursor.execute("ALTER TABLE employees ADD COLUMN salary REAL")
print("Table altered successfully.")
except sqlite3.OperationalError as e:
print("SQLite does not support adding columns with ALTER in some cases:", e)

# Step 5: Use DESCRIBE (SQLite alternative: PRAGMA table_info)


cursor.execute("PRAGMA table_info(employees)")
columns = cursor.fetchall()
print("Table structure:")
for column in columns:
print(column)

# Close the connection


conn.close()

You might also like