Skip to content

Instantly share code, notes, and snippets.

@vinaykudari
Last active August 22, 2018 18:11
Show Gist options
  • Save vinaykudari/fee28b7b73b2f5ff146ea9524fd527b9 to your computer and use it in GitHub Desktop.
Save vinaykudari/fee28b7b73b2f5ff146ea9524fd527b9 to your computer and use it in GitHub Desktop.
Basic Query
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"import sqlalchemy as db"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"engine = db.create_engine('sqlite:///census.sqlite')\n",
"connection = engine.connect()\n",
"metadata = db.MetaData()\n",
"census = db.Table('census', metadata, autoload=True, autoload_with=engine)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"#Equivalent to 'SELECT * FROM census'\n",
"query = db.select([census]) "
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"ResultProxy = connection.execute(query)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"ResultSet = ResultProxy.fetchall()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('Illinois', 'M', 0, 89600, 95012),\n",
" ('Illinois', 'M', 1, 88445, 91829),\n",
" ('Illinois', 'M', 2, 88729, 89547)]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ResultSet[:3]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment