Spaces:
Sleeping
Sleeping
File size: 827 Bytes
cffeaa1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import sqlite3
from typing import Any, List, Tuple
def run_query(db_path: str, query: str, params: Tuple = ()) -> List[Tuple[Any, ...]]:
"""
Runs a safe SQL query against a SQLite database and returns the result.
Args:
db_path (str): Full path to the SQLite db file.
query (str): SQL query string to execute.
params (tuple): Parameters for use in a parameterized SQL statement.
Returns:
List[Tuple]: Query results as rows (tuples). Empty list if no results or error.
"""
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute(query, params)
rows = cursor.fetchall()
conn.commit()
conn.close()
return rows
except sqlite3.Error as e:
print(f"[DB ERROR] {e}")
return [] |