ipython-sql
ipython-sql copied to clipboard
Variable substitution error when using cell magic
I am using ipython-sql 0.4.0 in a Google Colaboratory notebook.
Variable substitution using $ or {} when using the %%sql cell magic results in an error, e.g.
%%sql
GRANT CONNECT ON DATABASE postgres TO $username;
Error:
(psycopg2.ProgrammingError) syntax error at or near "$"
LINE 1: GRANT CONNECT ON DATABASE postgres TO $username;
But the same statement using a line magic works OK:
%sql GRANT CONNECT ON DATABASE postgres TO $username;
Same happens to me using a Jupyter notebook. myids is a list in
%sql CREATE TABLE boo AS SELECT id, title FROM document WHERE id IN {tuple(myids)}
which runs as expected by creating the boo table, but
%%sql
CREATE TABLE boo AS
SELECT id, title FROM document WHERE id IN {tuple(myids)}
fails with the error
/python3.7/site-packages/sql/magic.py in execute(self, line, cell, local_ns)
149 cell_params = {}
150 for variable in cell_variables:
--> 151 cell_params[variable] = local_ns[variable]
152 cell = cell.format(**cell_params)
KeyError: 'tuple(myids)'