Spaces:
Running
Running
add default uuid feature
#1
by
ZennyKenny
- opened
- database.py +12 -14
database.py
CHANGED
@@ -17,19 +17,21 @@ metadata_obj = MetaData()
|
|
17 |
def create_dynamic_table(df):
|
18 |
"""
|
19 |
Creates a table dynamically based on DataFrame schema.
|
20 |
-
|
21 |
Args:
|
22 |
df: pandas DataFrame containing the data
|
23 |
-
|
24 |
Returns:
|
25 |
SQLAlchemy Table object
|
26 |
"""
|
27 |
# Drop existing table if it exists
|
28 |
if 'data_table' in metadata_obj.tables:
|
29 |
metadata_obj.remove(metadata_obj.tables['data_table'])
|
30 |
-
|
31 |
-
#
|
32 |
-
columns = []
|
|
|
|
|
33 |
for col_name, dtype in df.dtypes.items():
|
34 |
if 'int' in str(dtype):
|
35 |
col_type = Integer
|
@@ -37,19 +39,15 @@ def create_dynamic_table(df):
|
|
37 |
col_type = Float
|
38 |
else:
|
39 |
col_type = String(255) # Using a generous length for string columns
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
columns.append(Column(col_name, col_type, primary_key=True))
|
44 |
-
else:
|
45 |
-
columns.append(Column(col_name, col_type))
|
46 |
-
|
47 |
# Create new table
|
48 |
table = Table('data_table', metadata_obj, *columns)
|
49 |
-
|
50 |
# Create table in database
|
51 |
metadata_obj.create_all(engine, tables=[table])
|
52 |
-
|
53 |
return table
|
54 |
|
55 |
def clear_database():
|
|
|
17 |
def create_dynamic_table(df):
|
18 |
"""
|
19 |
Creates a table dynamically based on DataFrame schema.
|
20 |
+
|
21 |
Args:
|
22 |
df: pandas DataFrame containing the data
|
23 |
+
|
24 |
Returns:
|
25 |
SQLAlchemy Table object
|
26 |
"""
|
27 |
# Drop existing table if it exists
|
28 |
if 'data_table' in metadata_obj.tables:
|
29 |
metadata_obj.remove(metadata_obj.tables['data_table'])
|
30 |
+
|
31 |
+
# Define columns with a new auto-incrementing primary key
|
32 |
+
columns = [Column("uuid", Integer, primary_key=True, autoincrement=True)] # New primary key
|
33 |
+
|
34 |
+
# Add columns based on DataFrame dtypes
|
35 |
for col_name, dtype in df.dtypes.items():
|
36 |
if 'int' in str(dtype):
|
37 |
col_type = Integer
|
|
|
39 |
col_type = Float
|
40 |
else:
|
41 |
col_type = String(255) # Using a generous length for string columns
|
42 |
+
|
43 |
+
columns.append(Column(col_name, col_type))
|
44 |
+
|
|
|
|
|
|
|
|
|
45 |
# Create new table
|
46 |
table = Table('data_table', metadata_obj, *columns)
|
47 |
+
|
48 |
# Create table in database
|
49 |
metadata_obj.create_all(engine, tables=[table])
|
50 |
+
|
51 |
return table
|
52 |
|
53 |
def clear_database():
|