SQL giving errors in jupyter

Hey guys,
I’m getting syntax error on the lines
%%capture and %%sql

Any help?

The screen Link: https://app.dataquest.io/m/257/guided-project%3A-analyzing-cia-factbook-data-using-sql/3/overview-of-the-data

The code:

%%capture
%load_ext sql
%sql sqlite:///factbook.db

Output:

File "<ipython-input-1-4656d1854217>", line 2
    %%capture
    ^
SyntaxError: invalid syntax
1 Like

Hi @carologira8 ,

Try the following approach: copy these 3 lines of code

%%capture
%load_ext sql
%sql sqlite:///factbook.db

in a separate code cell. Don’t put any other lines in this code cell, neigher below, nor above. Don’t put even comments, only these 3 lines. Next, refresh the kernel (Kernel - Restart), and after that run again this cell. It should work now.

2 Likes

That worked :blush:
Thank you @Elena_Kosourova

1 Like

Hey Elena, I run the following code in my jupyter notebook:
%%capture
%load_ext sql
%sql sqlite:///factbook.db

When I run this code I get nothing. no error, nothing. How do you recommend I proceed? I tried the trick of restarting the kernel and can’t get it to give me back a response.

Brian

Hi @bent1187,

The fact that you got no error means exactly that the code worked correctly :blush: It doesn’t give any output in Jupyter notebook, if you work on the project locally (but if you work on it inside the DQ mission, it would give the following output 'Connected: [email protected]', confirming the connection). It just means that your notebook is now connected to the database, and you can start using the database.

1 Like

Hey Elena,

Thanks for the quick reply! I do not get the “Connected: [email protected]”, however, I was able to run a sql query so I believe it is working! Quick question, how does jupyter where the Facebook.db file is at on my local computer? In the Python guided project I had to tell jupyter the exact file location of my .csv.

Brian

Hi Brian,

You didn’t receive that message “Connected: [email protected]” because you work on the project not from the DQ mission, but locally. And yes, that code works for sure.

This factbook.db database to which you are connected now is actually one of the files which you have downloaded from the DQ project mission (you used the Download bottom on the top right of the mission screen, then unzipped the folder and saved it somewhere on your computer, right?). In this way, you downloaded not only the 'Basics' notebook, but also factbook.db and the folder .ipynb_checkpoints. Now, when you open your 'Basics' file (it’s better to rename it in something more descriptive, though) from Jupyter, you have automatically access to all the other files in that project folder, hence you can easily connect to the factbook.db database.

1 Like

Of course, in case you haven’t downloaded the data from the mission yet, you have to do so from the Download bottom on the top right of the mission screen. Next, unzip the folder and save it with all its contents in some proper place on your computer. I mean, you should’t create a new Jupyter notebook, but open exactly the one which you downloaded (still empty 'Basics') and work from there, exactly in that folder.