sqlite3-interactive / pages /Task1_Create_Database_and_Tables.py
louiecerv's picture
saved changes
fe612fa
raw
history blame contribute delete
690 Bytes
import streamlit as st
import sqlite3
st.title("Task 1: Create a Database and Tables")
db_name = st.text_input("Enter database name:", "example.db")
table_name = st.text_input("Enter table name:", "students")
columns = st.text_area("Define columns (e.g., id INTEGER PRIMARY KEY, name TEXT):")
if st.button("Create Database and Table"):
try:
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute(f"CREATE TABLE IF NOT EXISTS {table_name} ({columns})")
conn.commit()
st.success(f"Table '{table_name}' created in database '{db_name}'.")
except Exception as e:
st.error(f"Error: {e}")
finally:
conn.close()