{"id":10260,"library":"sqlvalidator","title":"SQLValidator","description":"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.","status":"active","version":"0.0.20","language":"en","source_language":"en","source_url":"https://github.com/David-Wobrock/sqlvalidator","tags":["sql","validation","formatting","sqlparse"],"install":[{"cmd":"pip install sqlvalidator","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core parsing and formatting engine for SQL statements.","package":"sqlparse"}],"imports":[{"symbol":"SQLValidation","correct":"from sqlvalidator import SQLValidation"},{"note":"A convenient function for quick syntax validation without needing the SQLValidation object.","symbol":"validate_sql","correct":"from sqlvalidator import validate_sql"}],"quickstart":{"code":"from sqlvalidator import SQLValidation\n\n# Example 1: Basic syntax validation and formatting\nsql_query_1 = \"SELECT * FROM users WHERE id = 1 AND name = 'Alice'\"\nsql_obj_1 = SQLValidation(sql_query_1)\n\nprint(f\"Query 1 validation result: {sql_obj_1.validate()}\")\nprint(f\"Query 1 errors: {sql_obj_1.errors}\")\nprint(f\"Formatted Query 1:\\n{sql_obj_1.format()}\")\n\n# Example 2: Semantic validation with table schema\nsql_query_2 = \"SELECT first_name, email FROM employees WHERE department_id = 10\"\nsql_obj_2 = SQLValidation(sql_query_2)\n\n# Define table schema for semantic validation\ntables_schema = {\n    'employees': ['id', 'first_name', 'last_name', 'email', 'department_id'],\n    'departments': ['id', 'name']\n}\n\nsql_obj_2.validate(tables=tables_schema)\nprint(f\"\\nQuery 2 validation result (semantic): {sql_obj_2.is_valid}\")\nprint(f\"Query 2 errors (semantic): {sql_obj_2.errors}\")\n\n# Example 3: Invalid query\ninvalid_sql = \"SELECT FROM users WHERE id = 1\"\nsql_obj_3 = SQLValidation(invalid_sql)\nsql_obj_3.validate()\nprint(f\"\\nInvalid query errors: {sql_obj_3.errors}\")","lang":"python","description":"Demonstrates basic syntactic validation, formatting, and more advanced semantic validation by providing a table schema. The `is_valid` property and `errors` list provide detailed results."},"warnings":[{"fix":"Always provide the `tables` parameter for semantic validation: `sql_obj.validate(tables={'table_name': ['col1', 'col2']})`.","message":"Semantic validation (checking for valid table/column names) requires passing a `tables` dictionary to the `validate()` method. Without it, only syntactic validation is performed.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Pin your dependency to a specific minor version (`sqlvalidator==0.0.20`) and review changelogs when upgrading to a new minor version.","message":"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.","severity":"gotcha","affected_versions":"0.0.1 to 0.0.20"},{"fix":"If you have multiple queries, parse and validate them individually. Consider using `sqlparse.parse` to split complex scripts into individual statements first.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Add `from sqlvalidator import SQLValidation` at the top of your script.","cause":"The `SQLValidation` class was not imported.","error":"NameError: name 'SQLValidation' is not defined"},{"fix":"Pass a dictionary of table schemas to the `validate()` method: `sql_obj.validate(tables={'my_table': ['column1', 'column2']})`.","cause":"The `validate()` method was called without providing a `tables` dictionary for semantic context. By default, only syntactic validation is performed.","error":"Semantic validation not detecting unknown columns/tables (e.g., 'UnknownColumnError')"},{"fix":"Use `sql_obj.errors` instead of `sql_obj.error` to retrieve the list of validation errors.","cause":"Typo in accessing the errors list. The attribute name is `errors` (plural).","error":"AttributeError: 'SQLValidation' object has no attribute 'error'"}]}