Bug Description
When executing a Python cell that references a SQL variable injection (using @var syntax) in a DBCode notebook, the operation fails with two sequential errors:
- First Error (DBCode Output):
Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)
- Second Error (Python Execution):
Error: name 'test' is not defined
The issue occurs when DBCode attempts to inject SQL query results from a declared variable into the Python execution environment. The initial JSON parsing error prevents the variable from being injected, which subsequently causes the Python NameError when the code tries to reference the undefined variable.
Repro steps
- Open a DBCode notebook file (
.dbcnb)
- Create a SQL cell with a variable declaration:
-- @var test followed by a SQL query containing special characters (e.g., select top 20 * from Table where Column like '%test%';)
- Execute the SQL cell - it completes successfully and returns results
- Create a Python cell that references the injected variable (e.g.,
import pandas as pd and print(test.head()))
- Attempt to execute the Python cell
- Observe error in DBCode Output panel:
Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)
- Python cell execution fails with:
Error: name 'test' is not defined
Expected Behavior
- The SQL query results stored in the
test variable should be successfully injected into the Python cell's execution context as a pandas DataFrame
- The Python code should execute without errors and be able to access the data (e.g., calling
.head() method to display the first few rows)
- No JSON escape sequence errors should occur during the injection process
Actual Behavior
- DBCode fails to inject the SQL variable due to a JSON serialization error with escape sequences
- The DBCode Output panel shows:
Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)
- Because the injection fails, the variable is never created in the Python environment
- When the Python cell attempts to execute, it fails with:
Error: name 'test' is not defined
- This makes the Python injection feature completely non-functional for affected queries
Environment
- DBCode version: 1.19.9
- VS Code (or fork) version: 1.106.3 (user setup)
- OS: Windows
- Database: Microsoft SQL Server (MSSQL)
- Connection: Direct
Error Messages (if applicable)
DBCode Output Panel:
2025-12-04 17:56:37.940 [error] Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)
Python Cell Execution:
Error: name 'test' is not defined
Logs
2025-12-04 17:56:37.940 [error] Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)
Bug Description
When executing a Python cell that references a SQL variable injection (using
@varsyntax) in a DBCode notebook, the operation fails with two sequential errors:Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)Error: name 'test' is not definedThe issue occurs when DBCode attempts to inject SQL query results from a declared variable into the Python execution environment. The initial JSON parsing error prevents the variable from being injected, which subsequently causes the Python NameError when the code tries to reference the undefined variable.
Repro steps
.dbcnb)-- @var testfollowed by a SQL query containing special characters (e.g.,select top 20 * from Table where Column like '%test%';)import pandas as pdandprint(test.head()))Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)Error: name 'test' is not definedExpected Behavior
testvariable should be successfully injected into the Python cell's execution context as a pandas DataFrame.head()method to display the first few rows)Actual Behavior
Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)Error: name 'test' is not definedEnvironment
Error Messages (if applicable)
DBCode Output Panel:
Python Cell Execution:
Logs