Skip to content

Commit 6d08b94

Browse files
committed
Enhance SQLite interaction by adding DataFrame return option and create index listing script
1 parent d15b48c commit 6d08b94

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

anomstack/external/sqlite/sqlite.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,27 @@ def _action():
188188
return with_sqlite_retry(_action, logger=logger)
189189

190190

191-
def run_sql_sqlite(sql: str) -> None:
191+
def run_sql_sqlite(sql: str, return_df: bool = False):
192192
"""
193193
Execute a non-returning SQL statement (e.g. CREATE, INSERT, UPDATE, DELETE) with retry logic.
194194
195195
Args:
196196
sql (str): The SQL statement to execute.
197+
return_df (bool, optional): Whether to return the result as a DataFrame. Defaults to False.
197198
198199
Returns:
199200
None
200201
"""
201202
logger = get_dagster_logger()
202203
logger.info(f"Executing SQL against DB path: {get_sqlite_path()}")
203204

204-
def _action():
205+
def _action(return_df=return_df):
205206
with sqlite_connection() as conn:
206-
conn.execute(sql)
207+
cursor = conn.execute(sql)
207208
conn.commit()
209+
rows = cursor.fetchall()
210+
columns = [desc[0] for desc in cursor.description] if cursor.description else get_columns_from_sql(sql)
211+
df = pd.DataFrame(rows, columns=columns)
212+
return df if return_df else None
208213

209-
with_sqlite_retry(_action, logger=logger)
214+
return with_sqlite_retry(_action, logger=logger)

scripts/sqlite/list_indexes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from anomstack.external.sqlite.sqlite import run_sql_sqlite
2+
from dotenv import load_dotenv
3+
4+
load_dotenv(override=True)
5+
6+
7+
def list_indexes():
8+
query = "SELECT name, tbl_name FROM sqlite_master WHERE type='index' ORDER BY name;"
9+
df = run_sql_sqlite(query, return_df=True)
10+
print(df)
11+
12+
if __name__ == "__main__":
13+
df = list_indexes()
14+

scripts/sqlite/list_tables.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
from anomstack.external.sqlite.sqlite import run_sql_sqlite
22
from dotenv import load_dotenv
33

4-
load_dotenv()
4+
load_dotenv(override=True)
55

66

7-
def list_all_tables():
8-
# Query to get all table names from sqlite_master
9-
tables_query = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"
10-
11-
# Execute the query
12-
tables = run_sql_sqlite(tables_query)
13-
14-
if not tables:
15-
print("No tables found.")
16-
return
7+
def list_tables():
8+
query = "SELECT name, tbl_name FROM sqlite_master WHERE type='table' ORDER BY name;"
9+
df = run_sql_sqlite(query, return_df=True)
10+
print(df)
1711

18-
# Print the list of tables
19-
print("Tables in the database:")
20-
for table in tables:
21-
print(f"- {table[0]}") # Extract and print table name
22-
23-
# Run the function
24-
list_all_tables()
12+
if __name__ == "__main__":
13+
list_tables()

0 commit comments

Comments
 (0)