Skip to content

Instantly share code, notes, and snippets.

@albsen
Forked from catherinedevlin/SQL_Comparitor.ipynb
Created May 4, 2013 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albsen/5517340 to your computer and use it in GitHub Desktop.
Save albsen/5517340 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "SQL_Comparitor"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Side-by-side comparisons of query results across multiple database connections. Depends on [ipython-sql](https://pypi.python.org/pypi/ipython-sql)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext sql\n",
"import pandas as pd\n",
"from IPython.core.display import HTML\n",
"class SQL_Comparitor(object):\n",
" def __init__(self, *connection_strings):\n",
" self.connection_strings = connection_strings\n",
" def run(self, qry):\n",
" dframes = []\n",
" for connection_string in self.connection_strings:\n",
" result = %sql $connection_string $qry\n",
" short_name = connection_string.split('@')[1] \n",
" keys = [result.keys[0]]\n",
" for key in result.keys[1:]:\n",
" keys.append('%s_%s' % (short_name, key))\n",
" dframes.append(pd.DataFrame(result, columns=keys))\n",
" result = dframes[0]\n",
" for dframe in dframes[1:]:\n",
" result = pd.merge(result, dframe, on=keys[0])\n",
" return result"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"comparitor = SQL_Comparitor('mysql+pymysql://username:password@server1/db1', \n",
" 'mysql+pymysql://username:password@server2/db2',\n",
" )\n",
"results = comparitor.run(\"SHOW VARIABLES LIKE '%cache%' \")\n",
"HTML(results.to_html())"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment