notebook allow to save as sql file code example

Example: sql from jupyter notebooks

# import packages
import pandas as pd
import sqlite3 

# connect database and create cursor
conn = sqlite3.connect('data.sqlite')
cur = conn.cursor()

# executing the query
cur.execute("""SELECT * FROM employees LIMIT 5;""")

# creating a dataframe object
df = pd.DataFrame(cur.fetchall())

# find database attributes from cursor.description to use as column names
cur.description

# creating dataframe columns
df.columns = [x[0] for x in cur.description]
df.head()

Tags:

Sql Example