Work with SQL cells
PyCharm allows you to use SQL cells to query databases and dataframes in Jupyter notebooks and automatically save query results to pandas dataframes.
Enable the Database Tools and SQL plugin
This functionality relies on the Database Tools and SQL plugin, which is bundled and enabled in PyCharm by default. If the relevant features are not available, make sure that you did not disable the plugin.
Press Ctrl+Alt+S to open settings and then select
.Open the Installed tab, find the Database Tools and SQL plugin, and select the checkbox next to the plugin name.
Query a database
Select SQL on the toolbar in-between cells.
Click the widget on top of the SQL cell to select the data source or dataframe from the list:
To add a new data source, follow the steps below:
- Add a new data source
Open the Database tool window by clicking
Database or go to in the main menu.
Do one of the following:
Click
New in the Database tool window.
Press Alt+Insert.
Select Data Source and choose the database type.
Configure the connection settings in the Data Sources and Drivers dialog.
Set the name of the variable in which the result of the SQL request will be stored as a dataframe.
Enter the SQL statement and execute it by running the SQL cell.
To run the SQL cell, do one of the following:
Click
Run icon in the gutter.
Right-click the SQL cell and select
Run cell from the context menu.
Press Ctrl+Enter.
The query result will be shown in the output and saved to the dataframe.
Continue to work with the SQL variable in the code cells.