Jupyter Notebook does not allow me to type MySQL queries on multiple lines

Viewed 2335

As above, I cannot type my queries on multiple lines in Jupyter, which is annoying because it is harder to write and read my own queries. Is there a way to toggle multiple and single line input? I have googled quite a few times but the documentation doesn't seem much help.

PS: found the silly solution of typing '%%sql' instead of '%sql'

3 Answers

Building on above answer, use the pyodbc package, establish the connection, then query the database and bring data into Python memory.

import pyodbc
import pandas as pd

cnxn = pyodbc.connect('DSN=ODBC Connector Name', autocommit=True)

df = pd.read_sql_query("""
                       select
                         foo
                       from
                         bar
                       """,
                       cnxn
                       )

The %%sql solution did not work for me. Using \ at the end of the line did, though.

Example:

%sql SELECT column1, column2 \  
FROM table1 \  
WHERE column2 = (SELECT MIN(column2) FROM table1)
Related