Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2016-09-21T11:36:25.037811",
- "start_time": "2016-09-21T11:36:25.012904"
- },
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "from IPython.core.magic import (register_line_magic, register_cell_magic,\n",
- " register_line_cell_magic)\n",
- "from IPython.display import display\n",
- "from IPython.display import HTML\n",
- "import pandas as pd\n",
- "import pandas.io.sql as psql\n",
- "import psycopg2\n",
- "\n",
- "import credentials"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Connect to the database using credentials"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2016-09-21T11:36:25.816961",
- "start_time": "2016-09-21T11:36:25.787434"
- },
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "conn = psycopg2.connect(database=credentials.database,\n",
- " host=credentials.host,\n",
- " port=credentials.port,\n",
- " user=credentials.username,\n",
- " password=credentials.password)\n",
- "\n",
- "conn.autocommit = True"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Execute SQL command and query data from the database"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Create new table"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2016-09-21T11:39:33.977764",
- "start_time": "2016-09-21T11:39:33.838421"
- },
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "<cursor object at 0x115801430; closed: 0>"
- ]
- },
- "execution_count": 19,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "sql = '''\n",
- "DROP TABLE IF EXISTS example_data_table;\n",
- "CREATE TABLE example_data_table\n",
- "AS \n",
- "SELECT 1 col1, 2 col2, 3 col3;\n",
- "'''\n",
- "psql.execute(sql, conn)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Query results into a pandas dataframe"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2016-09-21T11:41:02.538333",
- "start_time": "2016-09-21T11:41:02.459945"
- },
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "sql = '''\n",
- "SELECT *\n",
- "FROM example_data_table\n",
- "'''\n",
- "df = psql.read_sql(sql, conn)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2016-09-21T11:41:02.716222",
- "start_time": "2016-09-21T11:41:02.676607"
- },
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>col1</th>\n",
- " <th>col2</th>\n",
- " <th>col3</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>0</th>\n",
- " <td>1</td>\n",
- " <td>2</td>\n",
- " <td>3</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " col1 col2 col3\n",
- "0 1 2 3"
- ]
- },
- "execution_count": 24,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Utilize Jupyter Magic\n",
- "### These magic commands are helpful for interacting with the database"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2016-09-21T11:41:36.853951",
- "start_time": "2016-09-21T11:41:36.826985"
- },
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "_df = None\n",
- "@register_cell_magic\n",
- "def showsql(line, cell):\n",
- " \"\"\"\n",
- " Extract the code in the specific cell (should be valid SQL), and execute\n",
- " it using the connection object to the backend database. \n",
- " The resulting Pandas dataframe\n",
- " is rendered inline below the cell using IPython.display.\n",
- " You'd use this for SELECT\n",
- " \"\"\"\n",
- " #Use the global connection object defined above.\n",
- " global conn\n",
- " global _df\n",
- " _df = psql.read_sql(cell, conn)\n",
- " conn.commit()\n",
- " display(_df)\n",
- " return\n",
- " \n",
- "@register_cell_magic\n",
- "def execsql(line, cell):\n",
- " \"\"\"\n",
- " Extract the code in the specific cell (should be valid SQL), and execute\n",
- " it using the connection object to the backend database. \n",
- " You'd use this for CREATE/UPDATE/DELETE\n",
- " \"\"\"\n",
- " #Use the global connection object defined above.\n",
- " global conn\n",
- " global _df\n",
- " _df = psql.execute(cell, conn)\n",
- " conn.commit()\n",
- " return\n",
- "\n",
- "# We delete these to avoid name conflicts for automagic to work\n",
- "del execsql, showsql"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2016-09-21T11:41:38.033078",
- "start_time": "2016-09-21T11:41:37.874356"
- },
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "%%execsql\n",
- "DROP TABLE IF EXISTS example_data_table;\n",
- "CREATE TABLE example_data_table\n",
- "AS \n",
- "SELECT 1 col1, 2 col2, 3 col3;"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2016-09-21T11:41:38.656976",
- "start_time": "2016-09-21T11:41:38.624547"
- },
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>col1</th>\n",
- " <th>col2</th>\n",
- " <th>col3</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>0</th>\n",
- " <td>1</td>\n",
- " <td>2</td>\n",
- " <td>3</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " col1 col2 col3\n",
- "0 1 2 3"
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- }
- ],
- "source": [
- "%%showsql\n",
- "SELECT *\n",
- "FROM example_data_table"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {
- "ExecuteTime": {
- "end_time": "2016-09-21T11:42:31.964638",
- "start_time": "2016-09-21T11:42:31.938667"
- },
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>col1</th>\n",
- " <th>col2</th>\n",
- " <th>col3</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>0</th>\n",
- " <td>1</td>\n",
- " <td>2</td>\n",
- " <td>3</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " col1 col2 col3\n",
- "0 1 2 3"
- ]
- },
- "execution_count": 29,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# global variable: result of query in pandas dataframe \n",
- "_df "
- ]
- }
- ],
- "metadata": {
- "anaconda-cloud": {},
- "kernelspec": {
- "display_name": "Python 2",
- "language": "python",
- "name": "python2"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 2
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.12"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement