In [1]:
import pandas as pd
import os
import os.path
import json
import sys
import importlib

module_dir = "../../modules"
module_path = os.path.abspath(module_dir)
if not module_path in sys.path:
    sys.path.append(module_path)

import dbutil
importlib.reload(dbutil)

%load_ext sql

Set User Credentials

Edit creds.json to reflect your mysql user and password

This must be done prior to executing the following cell

In [2]:
dbsource = "sqlite"
db = "book"
cstring = dbutil.db_cstring(dbsource, "creds.json", ".", db)
In [3]:
print("Connection string:", cstring)
Connection string: sqlite:///../../dbfiles/book.db

Establish Connection from Client to Server

The cell below uses the Python variable constructed earlier to actually establish a connection with the server.

If you cannot get a connection established, switch over to MySQL Workbench and try from that different client.

In [4]:
%sql $cstring

Make a Query

Try and execute the following line, which uses an "inline" sql magic to exectute a query to the database and retrieve the result.

In [5]:
%sql SELECT * from indicators0
 * sqlite:///../../dbfiles/book.db
Done.
Out[5]:
code pop gdp life cell
CHN 1386.4 12143.5 76.4 1469.88
FRA 66.87 2586.29 82.5 69.02
GBR 66.06 2637.87 81.2 79.1
IND 1338.66 2652.55 68.8 1168.9
USA 325.15 19485.4 78.5 391.6
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

Shift to a Different Database

In [6]:
dbsource = "sqlite"
db = "school"
cstring = dbutil.db_cstring(dbsource, "creds.json", ".", db)
print("Connection string:", cstring)
Connection string: sqlite:///../../dbfiles/school.db
In [7]:
%sql $cstring
In [ ]:
 
In [8]:
%sql SELECT * from departments
   sqlite:///../../dbfiles/book.db
 * sqlite:///../../dbfiles/school.db
Done.
Out[8]:
departmentid departmentname departmentchair division
ANSO Anthropology and Sociology None Social Sciences
ART Art History and Visual Culture 9252 Fine Arts
BIOL Biology None Natural Sciences
BLST Black Studies 9133 Interdisciplinary
CHEM Chemistry and Biochemistry 9143 Natural Sciences
CINE Cinema 9042 Fine Arts
CLAS Classical Studies 9111 Humanities
COMM Communication 9144 Social Sciences
DANC Dance 9180 Fine Arts
EAST East Asian Studies 9257 Interdisciplinary
ECON Economics 9291 Social Sciences
EDUC Education None Social Sciences
ENGL English 9142 Humanities
ENVS Environmental Studies 9253 Interdisciplinary
FYS First Year Seminar None Interdisciplinary
GEOS Geosciences None Natural Sciences
HIST History 9248 Humanities
HNRS Honors None Interdisciplinary
INTD Interdepartmental None Interdisciplinary
INTL International Studies 9178 Interdisciplinary
LACS Latin Amer &Caribbean Studies None Interdisciplinary
LANG Modern Language 9153 Humanities
MATH Mathematics & Computer Science 9140 Natural Sciences
MTA Media Technology and Arts None Interdisciplinary
MUS Music 9116 Fine Arts
NEUR Neuroscience None Interdisciplinary
PHED Health, Exercise, & Sport Stud 9073 Social Sciences
PHIL Philosophy 9085 Humanities
PHYS Physics 9089 Natural Sciences
POSC Political Science 9064 Social Sciences
PPE Philosophy/PoliSci/Economics None Interdisciplinary
PSYC Psychology 9107 Natural Sciences
QS Queer Studies None Interdisciplinary
REL Religion 9285 Humanities
THTR Theatre 9204 Fine Arts
WGST Women's and Gender Studies None Interdisciplinary
In [ ]: