Azizul Hakim
commited on
app.py
Browse files
app.py
CHANGED
@@ -111,6 +111,33 @@ def get_columns(db_name):
|
|
111 |
conn.close()
|
112 |
return columns, unique_columns
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
def delete_rows(db_name, condition_col, condition_val):
|
115 |
"""Delete rows based on condition"""
|
116 |
conn = sqlite3.connect(f'databases/{db_name}.db')
|
@@ -240,7 +267,7 @@ def main():
|
|
240 |
if unique_columns:
|
241 |
st.sidebar.warning(f"π« Unique Columns: {', '.join(unique_columns)}")
|
242 |
|
243 |
-
tabs = st.tabs(["π Manage Data", "π Update Data", "ποΈ Delete Data", "π₯ Import/Export"])
|
244 |
|
245 |
# Data Management Tab
|
246 |
with tabs[0]:
|
@@ -313,7 +340,10 @@ def main():
|
|
313 |
st.rerun()
|
314 |
else:
|
315 |
st.warning("No records were updated. Check your conditions.")
|
316 |
-
|
|
|
|
|
|
|
317 |
# Delete Data Tab
|
318 |
with tabs[2]:
|
319 |
st.subheader("Delete Records")
|
@@ -368,6 +398,36 @@ def main():
|
|
368 |
file_name=f"{selected_db}.json",
|
369 |
mime="application/json"
|
370 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
else:
|
372 |
st.info("π Welcome! Start by creating a new database using the sidebar.")
|
373 |
|
|
|
111 |
conn.close()
|
112 |
return columns, unique_columns
|
113 |
|
114 |
+
def rename_column(db_name, old_column_name, new_column_name):
|
115 |
+
"""Rename a column in the database"""
|
116 |
+
conn = sqlite3.connect(f'databases/{db_name}.db')
|
117 |
+
c = conn.cursor()
|
118 |
+
|
119 |
+
try:
|
120 |
+
# Get current table structure
|
121 |
+
c.execute("PRAGMA table_info(data)")
|
122 |
+
columns = [row[1] for row in c.fetchall()]
|
123 |
+
|
124 |
+
# Check if old column exists and new column doesn't
|
125 |
+
if old_column_name not in columns:
|
126 |
+
return False, "Original column does not exist"
|
127 |
+
if new_column_name in columns:
|
128 |
+
return False, "New column name already exists"
|
129 |
+
|
130 |
+
# Rename column using ALTER TABLE
|
131 |
+
c.execute(f"ALTER TABLE data RENAME COLUMN {old_column_name} TO {new_column_name}")
|
132 |
+
conn.commit()
|
133 |
+
return True, "Column renamed successfully"
|
134 |
+
|
135 |
+
except sqlite3.OperationalError as e:
|
136 |
+
return False, str(e)
|
137 |
+
finally:
|
138 |
+
conn.close()
|
139 |
+
|
140 |
+
|
141 |
def delete_rows(db_name, condition_col, condition_val):
|
142 |
"""Delete rows based on condition"""
|
143 |
conn = sqlite3.connect(f'databases/{db_name}.db')
|
|
|
267 |
if unique_columns:
|
268 |
st.sidebar.warning(f"π« Unique Columns: {', '.join(unique_columns)}")
|
269 |
|
270 |
+
tabs = st.tabs(["π Manage Data", "π Update Data", "ποΈ Delete Data", "π₯ Import/Export", "βοΈ Column Operations"])
|
271 |
|
272 |
# Data Management Tab
|
273 |
with tabs[0]:
|
|
|
340 |
st.rerun()
|
341 |
else:
|
342 |
st.warning("No records were updated. Check your conditions.")
|
343 |
+
|
344 |
+
|
345 |
+
|
346 |
+
|
347 |
# Delete Data Tab
|
348 |
with tabs[2]:
|
349 |
st.subheader("Delete Records")
|
|
|
398 |
file_name=f"{selected_db}.json",
|
399 |
mime="application/json"
|
400 |
)
|
401 |
+
|
402 |
+
|
403 |
+
with tabs[4]:
|
404 |
+
st.subheader("Column Operations")
|
405 |
+
|
406 |
+
# Rename Column Section
|
407 |
+
st.subheader("Rename Column")
|
408 |
+
col1, col2 = st.columns(2)
|
409 |
+
|
410 |
+
with col1:
|
411 |
+
old_column = st.selectbox("Select Column to Rename",
|
412 |
+
[col for col, _, _ in columns])
|
413 |
+
|
414 |
+
with col2:
|
415 |
+
new_column_name = st.text_input("Enter New Column Name")
|
416 |
+
|
417 |
+
if st.button("Rename Column"):
|
418 |
+
if new_column_name:
|
419 |
+
# Validate new name (no spaces, alphanumeric)
|
420 |
+
if not new_column_name.replace('_', '').isalnum():
|
421 |
+
st.error("Column name must be alphanumeric (can include underscores)")
|
422 |
+
else:
|
423 |
+
success, message = rename_column(selected_db, old_column, new_column_name)
|
424 |
+
if success:
|
425 |
+
st.success(message)
|
426 |
+
st.rerun()
|
427 |
+
else:
|
428 |
+
st.error(message)
|
429 |
+
else:
|
430 |
+
st.warning("Please enter a new column name")
|
431 |
else:
|
432 |
st.info("π Welcome! Start by creating a new database using the sidebar.")
|
433 |
|