Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +46 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
def generate_sql(file, operation, table_name):
|
| 5 |
+
if file is None or not table_name:
|
| 6 |
+
return "Please upload a CSV file and enter a table name."
|
| 7 |
+
|
| 8 |
+
df = pd.read_csv(file.name)
|
| 9 |
+
columns = df.columns.tolist()
|
| 10 |
+
queries = []
|
| 11 |
+
|
| 12 |
+
if operation == "ADD":
|
| 13 |
+
for _, row in df.iterrows():
|
| 14 |
+
values = tuple(row)
|
| 15 |
+
query = f"INSERT INTO {table_name} ({', '.join(columns)}) VALUES {values};"
|
| 16 |
+
queries.append(query)
|
| 17 |
+
|
| 18 |
+
elif operation == "UPDATE":
|
| 19 |
+
primary_key = columns[0] # Assuming the first column is the primary key
|
| 20 |
+
for _, row in df.iterrows():
|
| 21 |
+
set_clause = ', '.join([f"{col}='{row[col]}'" for col in columns[1:]])
|
| 22 |
+
query = f"UPDATE {table_name} SET {set_clause} WHERE {primary_key}='{row[primary_key]}';"
|
| 23 |
+
queries.append(query)
|
| 24 |
+
|
| 25 |
+
elif operation == "DELETE":
|
| 26 |
+
primary_key = columns[0]
|
| 27 |
+
for _, row in df.iterrows():
|
| 28 |
+
query = f"DELETE FROM {table_name} WHERE {primary_key}='{row[primary_key]}';"
|
| 29 |
+
queries.append(query)
|
| 30 |
+
|
| 31 |
+
return "\n".join(queries)
|
| 32 |
+
|
| 33 |
+
iface = gr.Interface(
|
| 34 |
+
fn=generate_sql,
|
| 35 |
+
inputs=[
|
| 36 |
+
gr.File(label="Upload CSV File"),
|
| 37 |
+
gr.Radio(["ADD", "UPDATE", "DELETE"], label="Select Operation"),
|
| 38 |
+
gr.Textbox(label="Enter Table Name")
|
| 39 |
+
],
|
| 40 |
+
outputs=gr.Textbox(label="Generated SQL Queries"),
|
| 41 |
+
title="CSV to SQL Query Converter",
|
| 42 |
+
description="<div style='text-align: center; margin-top: 20px;'>© 2025 Sandaru Abeykoon. All rights reserved.</div>",
|
| 43 |
+
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|