Last active
October 5, 2015 04:36
-
-
Save jiffyclub/e7e41382c80496e12e26 to your computer and use it in GitHub Desktop.
Simple SQLite inserts with SQLAlchemy and Odo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from sqlalchemy import Table, Column, create_engine, Integer, MetaData\n", | |
"from datashape import int_, Record, Option, var\n", | |
"from odo import resource, odo" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"data = [{'col1': x} for x in range(100, 103)]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### SQLAlchemy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"engine = create_engine('sqlite:///:memory:')\n", | |
"metadata = MetaData(bind=engine)\n", | |
"table = Table('testing', metadata, \n", | |
" Column('id', Integer, primary_key=True),\n", | |
" Column('col1', Integer))\n", | |
"metadata.create_all()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"with engine.contextual_connect() as conn:\n", | |
" conn.execute(table.insert(), data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(1, 100)\n", | |
"(2, 101)\n", | |
"(3, 102)\n" | |
] | |
} | |
], | |
"source": [ | |
"with engine.contextual_connect() as conn:\n", | |
" for row in conn.execute(table.select()):\n", | |
" print(row)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Table('testing', MetaData(bind=Engine(sqlite:///:memory:)), Column('id', Integer(), table=<testing>, primary_key=True, nullable=False), Column('col1', Integer(), table=<testing>), schema=None)" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"table" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Odo" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Table('testing', MetaData(bind=Engine(sqlite:///)), Column('id', Integer(), table=<testing>, primary_key=True, nullable=False), Column('col1', Integer(), table=<testing>, nullable=False), schema=None)" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dshape = var * Record([('id', int_), ('col1', int_)])\n", | |
"res = resource(\n", | |
" 'sqlite:///::testing', dshape=dshape, primary_key=['id'])\n", | |
"res" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Table('testing', MetaData(bind=Engine(sqlite:///)), Column('id', Integer(), table=<testing>, primary_key=True, nullable=False), Column('col1', Integer(), table=<testing>, nullable=False), schema=None)" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"odo(data, res)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[(1, 100), (2, 101), (3, 102)]" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"odo(res, list)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.5.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment