SQLValidator
SQLValidator is a Python library (current version 0.0.20) for formatting, syntactic, and semantic validation of SQL queries. It's built on `sqlparse` and provides a simple `SQLValidation` object and a helper `validate_sql` function. Releases are typically driven by new SQL feature support and bug fixes, with minor versions often adding functionality.
Common errors
-
NameError: name 'SQLValidation' is not defined
cause The `SQLValidation` class was not imported.fixAdd `from sqlvalidator import SQLValidation` at the top of your script. -
Semantic validation not detecting unknown columns/tables (e.g., 'UnknownColumnError')
cause The `validate()` method was called without providing a `tables` dictionary for semantic context. By default, only syntactic validation is performed.fixPass a dictionary of table schemas to the `validate()` method: `sql_obj.validate(tables={'my_table': ['column1', 'column2']})`. -
AttributeError: 'SQLValidation' object has no attribute 'error'
cause Typo in accessing the errors list. The attribute name is `errors` (plural).fixUse `sql_obj.errors` instead of `sql_obj.error` to retrieve the list of validation errors.
Warnings
- gotcha Semantic validation (checking for valid table/column names) requires passing a `tables` dictionary to the `validate()` method. Without it, only syntactic validation is performed.
- gotcha The library is in early `0.0.x` versions. While no major breaking changes are explicitly documented between minor releases yet, the API might evolve in future `0.x` versions before reaching a stable `1.0.0` release.
- gotcha The `SQLValidation` object processes a single SQL query string. Passing multiple concatenated queries (e.g., `"SELECT 1; SELECT 2;"`) may lead to unexpected parsing or validation results, as `sqlvalidator` focuses on validating individual statements.
Install
-
pip install sqlvalidator
Imports
- SQLValidation
from sqlvalidator import SQLValidation
- validate_sql
from sqlvalidator import validate_sql
Quickstart
from sqlvalidator import SQLValidation
# Example 1: Basic syntax validation and formatting
sql_query_1 = "SELECT * FROM users WHERE id = 1 AND name = 'Alice'"
sql_obj_1 = SQLValidation(sql_query_1)
print(f"Query 1 validation result: {sql_obj_1.validate()}")
print(f"Query 1 errors: {sql_obj_1.errors}")
print(f"Formatted Query 1:\n{sql_obj_1.format()}")
# Example 2: Semantic validation with table schema
sql_query_2 = "SELECT first_name, email FROM employees WHERE department_id = 10"
sql_obj_2 = SQLValidation(sql_query_2)
# Define table schema for semantic validation
tables_schema = {
'employees': ['id', 'first_name', 'last_name', 'email', 'department_id'],
'departments': ['id', 'name']
}
sql_obj_2.validate(tables=tables_schema)
print(f"\nQuery 2 validation result (semantic): {sql_obj_2.is_valid}")
print(f"Query 2 errors (semantic): {sql_obj_2.errors}")
# Example 3: Invalid query
invalid_sql = "SELECT FROM users WHERE id = 1"
sql_obj_3 = SQLValidation(invalid_sql)
sql_obj_3.validate()
print(f"\nInvalid query errors: {sql_obj_3.errors}")