Bases: SQLAlchemyDatabase
A SQLite database connection.
Can be initialized via a path to the database file.
Examples:
from blendsql.db import SQLite
db = SQLite("./path/to/database.db")
Source code in blendsql/db/_sqlite.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 | class SQLite(SQLAlchemyDatabase):
"""A SQLite database connection.
Can be initialized via a path to the database file.
Examples:
```python
from blendsql.db import SQLite
db = SQLite("./path/to/database.db")
```
"""
def __init__(self, db_path: str):
db_url: URL = make_url(f"sqlite:///{Path(db_path).resolve()}")
super().__init__(db_url=db_url)
def has_temp_table(self, tablename: str) -> bool:
return tablename in self.execute_to_list(
"SELECT name FROM sqlite_temp_master WHERE type='table';"
)
@cached_property
def sqlglot_schema(self) -> dict:
"""Returns database schema as a dictionary, in the format that
sqlglot.optimizer expects.
Examples:
>>> db.sqlglot_schema
{"x": {"A": "INT", "B": "INT", "C": "INT", "D": "INT", "Z": "STRING"}}
"""
schema: Dict[str, dict] = {}
for tablename in self.tables():
schema[f'"{double_quote_escape(tablename)}"'] = {}
for _, row in self.execute_to_df(
f"""
SELECT name, type FROM pragma_table_info(:t)
""",
{"t": tablename},
).iterrows():
schema[f'"{double_quote_escape(tablename)}"'][
'"' + row["name"] + '"'
] = row["type"]
return schema
|