Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'

like image 484
Caroline Tan Avatar asked Oct 26 '25 06:10

Caroline Tan


2 Answers

This is pretty easy to do using standard python syntax. Use the triple quote operator.

query = """
select
  foo
from
  bar
"""
like image 159
Jacobm001 Avatar answered Oct 28 '25 21:10

Jacobm001


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
                       )
like image 45
JCM Avatar answered Oct 28 '25 21:10

JCM