File size: 17,433 Bytes
1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 55a7a6f 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 3dcf2e7 1316cc2 3dcf2e7 1316cc2 3dcf2e7 1316cc2 1d831a4 3dcf2e7 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 55a7a6f 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 1316cc2 55a7a6f 1d831a4 1316cc2 1d831a4 1316cc2 1d831a4 55a7a6f 1d831a4 3dcf2e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
import streamlit as st
import pandas as pd
import sqlite3
import io
import base64
import os
from datetime import datetime
# Set page config
st.set_page_config(
page_title="Database Operations Tool",
page_icon="ποΈ",
layout="wide",
initial_sidebar_state="expanded"
)
# Modern UI styling
st.markdown("""
<style>
.main {
padding: 1rem;
}
.stButton>button {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 0.5rem;
border-radius: 5px;
border: none;
margin: 0.5rem 0;
}
.stButton>button:hover {
background-color: #45a049;
}
.reportview-container {
background: #fafafa;
}
.css-1d391kg {
padding: 1rem;
}
.stSelectbox {
margin: 1rem 0;
}
</style>
""", unsafe_allow_html=True)
def init_db():
"""Initialize SQLite database"""
if not os.path.exists('databases'):
os.makedirs('databases')
conn = sqlite3.connect('databases/main.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS database_list
(name TEXT PRIMARY KEY, created_date TEXT)''')
conn.commit()
return conn
def get_database_names():
"""Get list of databases"""
conn = init_db()
c = conn.cursor()
c.execute("SELECT name FROM database_list")
databases = [row[0] for row in c.fetchall()]
conn.close()
return databases
def create_table(db_name, columns, column_types, primary_key=None, unique_columns=None):
"""Create a new table with specified column types, optional primary key, and unique columns"""
conn = sqlite3.connect(f'databases/{db_name}.db')
c = conn.cursor()
# Prepare column definitions
column_defs = []
for col, type_ in zip(columns, column_types):
col_def = f"{col} {type_}"
# Set primary key
if primary_key and col == primary_key:
col_def += " PRIMARY KEY"
# Set unique columns
if unique_columns and col in unique_columns:
col_def += " UNIQUE"
column_defs.append(col_def)
query = f'''CREATE TABLE IF NOT EXISTS data
({', '.join(column_defs)})'''
c.execute(query)
conn.commit()
conn.close()
def get_table_data(db_name):
"""Get table data"""
conn = sqlite3.connect(f'databases/{db_name}.db')
return pd.read_sql_query("SELECT * FROM data", conn)
def get_columns(db_name):
"""Get column names and types"""
conn = sqlite3.connect(f'databases/{db_name}.db')
c = conn.cursor()
c.execute("PRAGMA table_info(data)")
columns = [(row[1], row[2], row[5] == 1) for row in c.fetchall()] # name, type, is_primary_key
# Check for unique columns
c.execute("PRAGMA index_list(data)")
unique_columns = [row[1].replace('data_', '').replace('_unique', '')
for row in c.fetchall() if 'unique' in row[1]]
conn.close()
return columns, unique_columns
def rename_column(db_name, old_column_name, new_column_name):
"""Rename a column in the database"""
conn = sqlite3.connect(f'databases/{db_name}.db')
c = conn.cursor()
try:
# Get current table structure
c.execute("PRAGMA table_info(data)")
columns = [row[1] for row in c.fetchall()]
# Check if old column exists and new column doesn't
if old_column_name not in columns:
return False, "Original column does not exist"
if new_column_name in columns:
return False, "New column name already exists"
# Rename column using ALTER TABLE
c.execute(f"ALTER TABLE data RENAME COLUMN {old_column_name} TO {new_column_name}")
conn.commit()
return True, "Column renamed successfully"
except sqlite3.OperationalError as e:
return False, str(e)
finally:
conn.close()
def delete_rows(db_name, condition_col, condition_val):
"""Delete rows based on condition"""
conn = sqlite3.connect(f'databases/{db_name}.db')
c = conn.cursor()
c.execute(f"DELETE FROM data WHERE {condition_col} = ?", (condition_val,))
deleted_count = c.rowcount
conn.commit()
conn.close()
return deleted_count
def update_row(db_name, condition_col, condition_val, update_col, update_val):
"""Update row based on condition with more flexible value setting"""
conn = sqlite3.connect(f'databases/{db_name}.db')
c = conn.cursor()
try:
# Update with exact value assignment
c.execute(f"UPDATE data SET {update_col} = ? WHERE {condition_col} = ?",
(update_val, condition_val))
updated_count = c.rowcount
conn.commit()
return updated_count
except sqlite3.IntegrityError as e:
st.error(f"Update failed: {str(e)}")
return 0
except sqlite3.OperationalError as e:
st.error(f"SQL Error: {str(e)}")
return 0
finally:
conn.close()
def bulk_import_data(db_name, df):
"""Bulk import data from DataFrame"""
conn = sqlite3.connect(f'databases/{db_name}.db')
try:
df.to_sql('data', conn, if_exists='append', index=False)
return True
except sqlite3.IntegrityError as e:
st.error(f"Import failed: {str(e)}")
return False
finally:
conn.close()
def main():
st.title("ποΈ Database Operations Tool")
# Sidebar for database operations
with st.sidebar:
st.header("Database Operations")
# Create New Database
with st.expander("Create New Database", expanded=True):
new_db_name = st.text_input("Database Name")
columns_input = st.text_input("Column Names (comma-separated)")
# Modified column type and primary key selection
if columns_input:
columns = [col.strip() for col in columns_input.split(",")]
column_types = []
# Create a select box for each column
st.write("Select type and configure each column:")
for col in columns:
col_type = st.selectbox(
f"Type for {col}",
options=["TEXT", "INTEGER", "REAL", "BLOB", "DATE"],
key=f"type_{col}" # Unique key for each selectbox
)
column_types.append(col_type)
# Primary key selection
primary_key = st.selectbox(
"Select Primary Key Column (optional)",
["None"] + columns,
index=0
)
primary_key = None if primary_key == "None" else primary_key
# Unique columns selection
unique_columns = st.multiselect(
"Select Unique Columns (optional)",
columns
)
if st.button("Create Database"):
if new_db_name and columns_input and len(columns) > 0:
try:
conn = init_db()
c = conn.cursor()
c.execute("INSERT INTO database_list VALUES (?, ?)",
(new_db_name, datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
conn.commit()
conn.close()
# Create table with primary key and unique columns
create_table(
new_db_name,
columns,
column_types,
primary_key,
unique_columns
)
st.success("β
Database created successfully!")
except sqlite3.IntegrityError:
st.error("β Database name already exists!")
else:
st.warning("β οΈ Please fill all fields")
# Main content area
databases = get_database_names()
if databases:
selected_db = st.selectbox("π Select Database", databases)
if selected_db:
df = get_table_data(selected_db)
columns, unique_columns = get_columns(selected_db)
# Display current table constraints
st.sidebar.subheader("Table Constraints")
columns_info = [col for col, _, _ in columns]
# Highlight primary key and unique columns
primary_key = next((col for col, _, is_primary in columns if is_primary), None)
if primary_key:
st.sidebar.info(f"π Primary Key: {primary_key}")
if unique_columns:
st.sidebar.warning(f"π« Unique Columns: {', '.join(unique_columns)}")
tabs = st.tabs(["π Manage Data", "π Update Data", "ποΈ Delete Data", "π₯ Import/Export", "βοΈ Column Operations"])
# Data Management Tab
with tabs[0]:
st.subheader("Data Preview")
st.dataframe(df, use_container_width=True)
st.subheader("Add New Row")
new_row_data = {}
for col, type_, is_primary in columns:
if type_ == 'DATE':
new_row_data[col] = st.date_input(f"Enter {col}")
elif type_ == 'INTEGER':
new_row_data[col] = st.number_input(f"Enter {col}", step=1)
elif type_ == 'REAL':
new_row_data[col] = st.number_input(f"Enter {col}", step=0.1)
else:
new_row_data[col] = st.text_input(f"Enter {col}")
if st.button("Add Row"):
if all(str(val) != "" for val in new_row_data.values()):
conn = sqlite3.connect(f'databases/{selected_db}.db')
c = conn.cursor()
try:
placeholders = ','.join(['?' for _ in columns])
query = f"INSERT INTO data VALUES ({placeholders})"
c.execute(query, list(new_row_data.values()))
conn.commit()
st.success("β
Row added!")
st.rerun()
except sqlite3.IntegrityError as e:
st.error(f"β Insert failed: {str(e)}")
finally:
conn.close()
# Update Data Tab
with tabs[1]:
st.subheader("Update Records")
col1, col2 = st.columns(2)
with col1:
condition_col = st.selectbox("Select Column for Condition",
[col for col, _, _ in columns])
condition_val = st.text_input("Enter Value to Match")
with col2:
update_col = st.selectbox("Select Column to Update",
[col for col, _, _ in columns])
# Different input method based on column type
column_types_dict = {col: type_ for col, type_, _ in columns}
update_val_input = None
if column_types_dict[update_col] == 'INTEGER':
update_val_input = st.number_input(f"Enter New Value for {update_col}", step=1)
elif column_types_dict[update_col] == 'REAL':
update_val_input = st.number_input(f"Enter New Value for {update_col}", step=0.1)
elif column_types_dict[update_col] == 'DATE':
update_val_input = st.date_input(f"Enter New Value for {update_col}")
else:
update_val_input = st.text_input(f"Enter New Value for {update_col}")
if st.button("Update Records"):
if condition_val is not None and update_val_input is not None:
# Convert update_val_input to string to work with SQLite
update_val = str(update_val_input)
updated = update_row(selected_db, condition_col,
condition_val, update_col, update_val)
if updated > 0:
st.success(f"β
Updated {updated} records!")
st.rerun()
else:
st.warning("No records were updated. Check your conditions.")
# Delete Data Tab
with tabs[2]:
st.subheader("Delete Records")
del_col = st.selectbox("Select Column for Deletion Condition",
[col for col, _, _ in columns])
del_val = st.text_input("Enter Value to Delete")
if st.button("Delete Records"):
if del_val:
deleted = delete_rows(selected_db, del_col, del_val)
st.success(f"β
Deleted {deleted} records!")
st.rerun()
# Import/Export Tab
with tabs[3]:
st.subheader("Import Data")
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
if uploaded_file is not None:
import_df = pd.read_csv(uploaded_file)
if st.button("Import Data"):
success = bulk_import_data(selected_db, import_df)
if success:
st.success("β
Data imported successfully!")
st.rerun()
st.subheader("Export Data")
export_format = st.selectbox("Select Format",
["CSV", "Excel", "JSON"])
if export_format == "CSV":
csv = df.to_csv(index=False)
st.download_button(
label="π₯ Download CSV",
data=csv,
file_name=f"{selected_db}.csv",
mime="text/csv"
)
elif export_format == "Excel":
buffer = io.BytesIO()
df.to_excel(buffer, index=False)
st.download_button(
label="π₯ Download Excel",
data=buffer.getvalue(),
file_name=f"{selected_db}.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
else: # JSON
json_str = df.to_json(orient='records')
st.download_button(
label="π₯ Download JSON",
data=json_str,
file_name=f"{selected_db}.json",
mime="application/json"
)
with tabs[4]:
st.subheader("Column Operations")
# Rename Column Section
st.subheader("Rename Column")
col1, col2 = st.columns(2)
with col1:
old_column = st.selectbox("Select Column to Rename",
[col for col, _, _ in columns])
with col2:
new_column_name = st.text_input("Enter New Column Name")
if st.button("Rename Column"):
if new_column_name:
# Validate new name (no spaces, alphanumeric)
if not new_column_name.replace('_', '').isalnum():
st.error("Column name must be alphanumeric (can include underscores)")
else:
success, message = rename_column(selected_db, old_column, new_column_name)
if success:
st.success(message)
st.rerun()
else:
st.error(message)
else:
st.warning("Please enter a new column name")
else:
st.info("π Welcome! Start by creating a new database using the sidebar.")
if __name__ == "__main__":
main()
|