{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# https://docs.python.jp/3/library/sqlite3.html" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import sqlite3" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "path = ':memory:'\n", "conn = sqlite3.connect(path)\n", "\n", "c = conn.cursor()\n", "\n", "c.execute(\"DROP TABLE IF EXISTS items;\")\n", "c.execute('''CREATE TABLE items(\n", " id INTEGER PRIMARY KEY,\n", " name TEXT,\n", " value INTEGER)''')\n", "\n", "c.execute(\"INSERT INTO items (name, value) VALUES (?, ?)\", ('one', 100))\n", "c.execute(\"INSERT INTO items (name, value) VALUES (?, ?)\", ('two', 200))\n", "c.execute(\"INSERT INTO items (name, value) VALUES (?, ?)\", ('three', 300))\n", "\n", "data = [('four', 400), ('five', 500), ('six', 600)]\n", "c.executemany(\"INSERT INTO items (name, value) VALUES (?, ?)\", data)\n", "\n", "conn.commit()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(1, 'one', 100), (2, 'two', 200), (3, 'three', 300), (4, 'four', 400), (5, 'five', 500), (6, 'six', 600)]\n" ] } ], "source": [ "c.execute(\"SELECT * FROM items\")\n", "print(c.fetchall())" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(4, 'four', 400), (5, 'five', 500), (6, 'six', 600)]\n" ] } ], "source": [ "c.execute(\"SELECT * FROM items WHERE id >= ?\", (4, ))\n", "print(c.fetchall())" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "conn.close()" ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 2 }