Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to pass a python variable to a multine sql magic #120

Closed
aborruso opened this issue Jun 6, 2018 · 4 comments
Closed

how to pass a python variable to a multine sql magic #120

aborruso opened this issue Jun 6, 2018 · 4 comments

Comments

@aborruso
Copy link

aborruso commented Jun 6, 2018

Hi,
is it possible to have

myvar="white"

And use it in

%%sql
SELECT * from mytable where color LIKE $myvar;

I'm not able to do use it in a multiline magic

@aborruso
Copy link
Author

It's a duplicate of #80

@muggle14
Copy link

Has this been published into the package now?

@mengxi-ream
Copy link

Has this been published into the package now?

This problem has been resolved in 0.3.9

Just use : before the variable

For multiline SQL

name = 'Countess'

%%sql
select description 
from character 
where charname = :name
# output: [(u'mother to Bertram',)]

You can also use : in single line SQL

name = 'Countess'

%sql select description from character where charname = :name
# output: [(u'mother to Bertram',)]

@yoonghm
Copy link

yoonghm commented Feb 3, 2023

The following does not work:

t = 'employee'
%sql SELECT * FROM :t

The error messages are:

* sqlite://
(sqlite3.OperationalError) near "?": syntax error
[SQL: SELECT * FROM ?]
[parameters: ('employee',)]
(Background on this error at: https://sqlalche.me/e/14/e3q8)

However, this works:

t = 'employee'
%sql SELECT * FROM $t

pmfischer pushed a commit to pmfischer/ipython-sql that referenced this issue Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants