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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import sqlite3
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
import os
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
# Set page config
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Database Operations Tool",
|
| 12 |
+
page_icon="ποΈ",
|
| 13 |
+
layout="wide",
|
| 14 |
+
initial_sidebar_state="expanded"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Modern UI styling
|
| 18 |
+
st.markdown("""
|
| 19 |
+
<style>
|
| 20 |
+
.main {
|
| 21 |
+
padding: 1rem;
|
| 22 |
+
}
|
| 23 |
+
.stButton>button {
|
| 24 |
+
width: 100%;
|
| 25 |
+
background-color: #4CAF50;
|
| 26 |
+
color: white;
|
| 27 |
+
padding: 0.5rem;
|
| 28 |
+
border-radius: 5px;
|
| 29 |
+
border: none;
|
| 30 |
+
margin: 0.5rem 0;
|
| 31 |
+
}
|
| 32 |
+
.stButton>button:hover {
|
| 33 |
+
background-color: #45a049;
|
| 34 |
+
}
|
| 35 |
+
.reportview-container {
|
| 36 |
+
background: #fafafa;
|
| 37 |
+
}
|
| 38 |
+
.css-1d391kg {
|
| 39 |
+
padding: 1rem;
|
| 40 |
+
}
|
| 41 |
+
.stSelectbox {
|
| 42 |
+
margin: 1rem 0;
|
| 43 |
+
}
|
| 44 |
+
</style>
|
| 45 |
+
""", unsafe_allow_html=True)
|
| 46 |
+
|
| 47 |
+
def init_db():
|
| 48 |
+
"""Initialize SQLite database"""
|
| 49 |
+
if not os.path.exists('databases'):
|
| 50 |
+
os.makedirs('databases')
|
| 51 |
+
conn = sqlite3.connect('databases/main.db')
|
| 52 |
+
c = conn.cursor()
|
| 53 |
+
c.execute('''CREATE TABLE IF NOT EXISTS database_list
|
| 54 |
+
(name TEXT PRIMARY KEY, created_date TEXT)''')
|
| 55 |
+
conn.commit()
|
| 56 |
+
return conn
|
| 57 |
+
|
| 58 |
+
def get_database_names():
|
| 59 |
+
"""Get list of databases"""
|
| 60 |
+
conn = init_db()
|
| 61 |
+
c = conn.cursor()
|
| 62 |
+
c.execute("SELECT name FROM database_list")
|
| 63 |
+
databases = [row[0] for row in c.fetchall()]
|
| 64 |
+
conn.close()
|
| 65 |
+
return databases
|
| 66 |
+
|
| 67 |
+
def create_table(db_name, columns, column_types):
|
| 68 |
+
"""Create a new table with specified column types"""
|
| 69 |
+
conn = sqlite3.connect(f'databases/{db_name}.db')
|
| 70 |
+
c = conn.cursor()
|
| 71 |
+
columns_with_types = [f"{col} {type_}" for col, type_ in zip(columns, column_types)]
|
| 72 |
+
query = f'''CREATE TABLE IF NOT EXISTS data
|
| 73 |
+
({', '.join(columns_with_types)})'''
|
| 74 |
+
c.execute(query)
|
| 75 |
+
conn.commit()
|
| 76 |
+
conn.close()
|
| 77 |
+
|
| 78 |
+
def get_table_data(db_name):
|
| 79 |
+
"""Get table data"""
|
| 80 |
+
conn = sqlite3.connect(f'databases/{db_name}.db')
|
| 81 |
+
return pd.read_sql_query("SELECT * FROM data", conn)
|
| 82 |
+
|
| 83 |
+
def get_columns(db_name):
|
| 84 |
+
"""Get column names and types"""
|
| 85 |
+
conn = sqlite3.connect(f'databases/{db_name}.db')
|
| 86 |
+
c = conn.cursor()
|
| 87 |
+
c.execute("PRAGMA table_info(data)")
|
| 88 |
+
columns = [(row[1], row[2]) for row in c.fetchall()]
|
| 89 |
+
conn.close()
|
| 90 |
+
return columns
|
| 91 |
+
|
| 92 |
+
def delete_rows(db_name, condition_col, condition_val):
|
| 93 |
+
"""Delete rows based on condition"""
|
| 94 |
+
conn = sqlite3.connect(f'databases/{db_name}.db')
|
| 95 |
+
c = conn.cursor()
|
| 96 |
+
c.execute(f"DELETE FROM data WHERE {condition_col} = ?", (condition_val,))
|
| 97 |
+
deleted_count = c.rowcount
|
| 98 |
+
conn.commit()
|
| 99 |
+
conn.close()
|
| 100 |
+
return deleted_count
|
| 101 |
+
|
| 102 |
+
def update_row(db_name, condition_col, condition_val, update_col, update_val):
|
| 103 |
+
"""Update row based on condition"""
|
| 104 |
+
conn = sqlite3.connect(f'databases/{db_name}.db')
|
| 105 |
+
c = conn.cursor()
|
| 106 |
+
c.execute(f"UPDATE data SET {update_col} = ? WHERE {condition_col} = ?",
|
| 107 |
+
(update_val, condition_val))
|
| 108 |
+
updated_count = c.rowcount
|
| 109 |
+
conn.commit()
|
| 110 |
+
conn.close()
|
| 111 |
+
return updated_count
|
| 112 |
+
|
| 113 |
+
def bulk_import_data(db_name, df):
|
| 114 |
+
"""Bulk import data from DataFrame"""
|
| 115 |
+
conn = sqlite3.connect(f'databases/{db_name}.db')
|
| 116 |
+
df.to_sql('data', conn, if_exists='append', index=False)
|
| 117 |
+
conn.close()
|
| 118 |
+
|
| 119 |
+
def main():
|
| 120 |
+
st.title("ποΈ Database Operations Tool")
|
| 121 |
+
|
| 122 |
+
# Sidebar for database operations
|
| 123 |
+
with st.sidebar:
|
| 124 |
+
st.header("Database Operations")
|
| 125 |
+
|
| 126 |
+
# Create New Database
|
| 127 |
+
with st.expander("Create New Database", expanded=True):
|
| 128 |
+
new_db_name = st.text_input("Database Name")
|
| 129 |
+
columns_input = st.text_input("Column Names (comma-separated)")
|
| 130 |
+
column_types = st.multiselect("Column Types",
|
| 131 |
+
["TEXT", "INTEGER", "REAL", "BLOB", "DATE"],
|
| 132 |
+
["TEXT"])
|
| 133 |
+
|
| 134 |
+
if st.button("Create Database"):
|
| 135 |
+
if new_db_name and columns_input and column_types:
|
| 136 |
+
try:
|
| 137 |
+
columns = [col.strip() for col in columns_input.split(",")]
|
| 138 |
+
if len(columns) != len(column_types):
|
| 139 |
+
st.error("β Number of columns and types must match!")
|
| 140 |
+
return
|
| 141 |
+
|
| 142 |
+
conn = init_db()
|
| 143 |
+
c = conn.cursor()
|
| 144 |
+
c.execute("INSERT INTO database_list VALUES (?, ?)",
|
| 145 |
+
(new_db_name, datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
|
| 146 |
+
conn.commit()
|
| 147 |
+
conn.close()
|
| 148 |
+
create_table(new_db_name, columns, column_types)
|
| 149 |
+
st.success("β
Database created successfully!")
|
| 150 |
+
except sqlite3.IntegrityError:
|
| 151 |
+
st.error("β Database name already exists!")
|
| 152 |
+
else:
|
| 153 |
+
st.warning("β οΈ Please fill all fields")
|
| 154 |
+
|
| 155 |
+
# Main content area
|
| 156 |
+
databases = get_database_names()
|
| 157 |
+
if databases:
|
| 158 |
+
selected_db = st.selectbox("π Select Database", databases)
|
| 159 |
+
|
| 160 |
+
if selected_db:
|
| 161 |
+
df = get_table_data(selected_db)
|
| 162 |
+
columns = get_columns(selected_db)
|
| 163 |
+
|
| 164 |
+
tabs = st.tabs(["π Manage Data", "π Update Data", "ποΈ Delete Data", "π₯ Import/Export"])
|
| 165 |
+
|
| 166 |
+
# Data Management Tab
|
| 167 |
+
with tabs[0]:
|
| 168 |
+
st.subheader("Data Preview")
|
| 169 |
+
st.dataframe(df, use_container_width=True)
|
| 170 |
+
|
| 171 |
+
st.subheader("Add New Row")
|
| 172 |
+
new_row_data = {}
|
| 173 |
+
for col, type_ in columns:
|
| 174 |
+
if type_ == 'DATE':
|
| 175 |
+
new_row_data[col] = st.date_input(f"Enter {col}")
|
| 176 |
+
elif type_ == 'INTEGER':
|
| 177 |
+
new_row_data[col] = st.number_input(f"Enter {col}", step=1)
|
| 178 |
+
elif type_ == 'REAL':
|
| 179 |
+
new_row_data[col] = st.number_input(f"Enter {col}", step=0.1)
|
| 180 |
+
else:
|
| 181 |
+
new_row_data[col] = st.text_input(f"Enter {col}")
|
| 182 |
+
|
| 183 |
+
if st.button("Add Row"):
|
| 184 |
+
if all(str(val) != "" for val in new_row_data.values()):
|
| 185 |
+
conn = sqlite3.connect(f'databases/{selected_db}.db')
|
| 186 |
+
c = conn.cursor()
|
| 187 |
+
placeholders = ','.join(['?' for _ in columns])
|
| 188 |
+
query = f"INSERT INTO data VALUES ({placeholders})"
|
| 189 |
+
c.execute(query, list(new_row_data.values()))
|
| 190 |
+
conn.commit()
|
| 191 |
+
conn.close()
|
| 192 |
+
st.success("β
Row added!")
|
| 193 |
+
st.rerun()
|
| 194 |
+
|
| 195 |
+
# Update Data Tab
|
| 196 |
+
with tabs[1]:
|
| 197 |
+
st.subheader("Update Records")
|
| 198 |
+
col1, col2 = st.columns(2)
|
| 199 |
+
|
| 200 |
+
with col1:
|
| 201 |
+
condition_col = st.selectbox("Select Column for Condition",
|
| 202 |
+
[col for col, _ in columns])
|
| 203 |
+
condition_val = st.text_input("Enter Value to Match")
|
| 204 |
+
|
| 205 |
+
with col2:
|
| 206 |
+
update_col = st.selectbox("Select Column to Update",
|
| 207 |
+
[col for col, _ in columns])
|
| 208 |
+
update_val = st.text_input("Enter New Value")
|
| 209 |
+
|
| 210 |
+
if st.button("Update Records"):
|
| 211 |
+
if condition_val and update_val:
|
| 212 |
+
updated = update_row(selected_db, condition_col,
|
| 213 |
+
condition_val, update_col, update_val)
|
| 214 |
+
st.success(f"β
Updated {updated} records!")
|
| 215 |
+
st.rerun()
|
| 216 |
+
|
| 217 |
+
# Delete Data Tab
|
| 218 |
+
with tabs[2]:
|
| 219 |
+
st.subheader("Delete Records")
|
| 220 |
+
del_col = st.selectbox("Select Column for Deletion Condition",
|
| 221 |
+
[col for col, _ in columns])
|
| 222 |
+
del_val = st.text_input("Enter Value to Delete")
|
| 223 |
+
|
| 224 |
+
if st.button("Delete Records"):
|
| 225 |
+
if del_val:
|
| 226 |
+
deleted = delete_rows(selected_db, del_col, del_val)
|
| 227 |
+
st.success(f"β
Deleted {deleted} records!")
|
| 228 |
+
st.rerun()
|
| 229 |
+
|
| 230 |
+
# Import/Export Tab
|
| 231 |
+
with tabs[3]:
|
| 232 |
+
st.subheader("Import Data")
|
| 233 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
| 234 |
+
if uploaded_file is not None:
|
| 235 |
+
import_df = pd.read_csv(uploaded_file)
|
| 236 |
+
if st.button("Import Data"):
|
| 237 |
+
bulk_import_data(selected_db, import_df)
|
| 238 |
+
st.success("β
Data imported successfully!")
|
| 239 |
+
st.rerun()
|
| 240 |
+
|
| 241 |
+
st.subheader("Export Data")
|
| 242 |
+
export_format = st.selectbox("Select Format",
|
| 243 |
+
["CSV", "Excel", "JSON"])
|
| 244 |
+
|
| 245 |
+
if export_format == "CSV":
|
| 246 |
+
csv = df.to_csv(index=False)
|
| 247 |
+
st.download_button(
|
| 248 |
+
label="π₯ Download CSV",
|
| 249 |
+
data=csv,
|
| 250 |
+
file_name=f"{selected_db}.csv",
|
| 251 |
+
mime="text/csv"
|
| 252 |
+
)
|
| 253 |
+
elif export_format == "Excel":
|
| 254 |
+
buffer = io.BytesIO()
|
| 255 |
+
df.to_excel(buffer, index=False)
|
| 256 |
+
st.download_button(
|
| 257 |
+
label="π₯ Download Excel",
|
| 258 |
+
data=buffer.getvalue(),
|
| 259 |
+
file_name=f"{selected_db}.xlsx",
|
| 260 |
+
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 261 |
+
)
|
| 262 |
+
else: # JSON
|
| 263 |
+
json_str = df.to_json(orient='records')
|
| 264 |
+
st.download_button(
|
| 265 |
+
label="π₯ Download JSON",
|
| 266 |
+
data=json_str,
|
| 267 |
+
file_name=f"{selected_db}.json",
|
| 268 |
+
mime="application/json"
|
| 269 |
+
)
|
| 270 |
+
else:
|
| 271 |
+
st.info("π Welcome! Start by creating a new database using the sidebar.")
|
| 272 |
+
|
| 273 |
+
if __name__ == "__main__":
|
| 274 |
+
main()
|
requirements.txt.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
sqlite3
|
| 4 |
+
base64
|
| 5 |
+
datetime
|