Download src/utils/schema_extractor.py from maikheb/nl2sql: direct link, hf CLI and curl.
- Browser
- Download file 789 Bytes
-
https://huggingface.co/spaces/maikheb/nl2sql/resolve/main/src/utils/schema_extractor.py
- Command line
-
hf download hf://spaces/maikheb/nl2sql/src/utils/schema_extractor.py
-
curl -L -o schema_extractor.py https://huggingface.co/spaces/maikheb/nl2sql/resolve/main/src/utils/schema_extractor.py
789 Bytes
| from sqlalchemy import create_engine, inspect | |
| import sqlite3 | |
| def extract_schema_sqlite(db_path): | |
| conn = sqlite3.connect(db_path) | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") | |
| tables = [row[0] for row in cursor.fetchall()] | |
| schema = {} | |
| for table in tables: | |
| cursor.execute(f"PRAGMA table_info({table});") | |
| schema[table] = [col[1] for col in cursor.fetchall()] | |
| conn.close() | |
| return schema | |
| def extract_schema_rdbms(db_uri): | |
| engine = create_engine(db_uri) | |
| inspector = inspect(engine) | |
| schema = {} | |
| for table in inspector.get_table_names(): | |
| columns = [col["name"] for col in inspector.get_columns(table)] | |
| schema[table] = columns | |
| return schema | |