Commits

Matt Bone  committed 65d3949

latest db example

  • Participants
  • Parent commits e915fac

Comments (0)

Files changed (1)

File database_examples.ipynb

      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 2
+     "prompt_number": 1
     },
     {
      "cell_type": "code",
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 3
+     "prompt_number": 2
     },
     {
      "cell_type": "code",
      "metadata": {},
      "outputs": [
       {
-       "ename": "OperationalError",
-       "evalue": "table students already exists",
-       "output_type": "pyerr",
-       "traceback": [
-        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mOperationalError\u001b[0m                          Traceback (most recent call last)",
-        "\u001b[1;32m<ipython-input-5-53886e308ce1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m     \u001b[0mage\u001b[0m \u001b[0mSMALLINT\u001b[0m \u001b[0mNOT\u001b[0m \u001b[0mNULL\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m     PRIMARY KEY (id));\n\u001b[1;32m----> 8\u001b[1;33m \"\"\")\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m c.execute(\"\"\"CREATE TABLE grades (\n",
-        "\u001b[1;31mOperationalError\u001b[0m: table students already exists"
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 3,
+       "text": [
+        "<sqlite3.Cursor at 0x1506810>"
        ]
       }
      ],
+     "prompt_number": 3
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "student_data = [\n",
+      "                {'id': 1, 'name': 'matt', 'age': 29},\n",
+      "                {'id': 2, 'name': 'alice', 'age': 33},\n",
+      "                {'id': 3, 'name': 'same', 'age': 25},\n",
+      "                ]"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 4
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "grade_data = [\n",
+      "              {'id': 1, 'course': 'markup', 'grade': '85', 'student_id': 1},\n",
+      "              {'id': 2, 'course': 'compilers', 'grade': '77', 'student_id': 1},\n",
+      "              {'id': 3, 'course': 'markup', 'grade': '90', 'student_id': 2},\n",
+      "              {'id': 4, 'course': 'compilers', 'grade': '95', 'student_id': 2},\n",
+      "              {'id': 5, 'course': 'markup', 'grade': '85', 'student_id': 3},\n",
+      "              {'id': 6, 'course': 'operating systems', 'grade': '82', 'student_id': 3}\n",
+      "              ]           "
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
      "prompt_number": 5
     },
     {
      "cell_type": "code",
      "collapsed": false,
+     "input": [
+      "c.executemany('INSERT INTO students VALUES (?,?,?)', [(s['id'], s['name'], s['age']) for s in student_data])"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 6,
+       "text": [
+        "<sqlite3.Cursor at 0x1506810>"
+       ]
+      }
+     ],
+     "prompt_number": 6
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "c.executemany('INSERT INTO grades VALUES (?,?,?,?)', [(s['id'], s['course'], s['grade'], s['student_id']) for s in grade_data])"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 8,
+       "text": [
+        "<sqlite3.Cursor at 0x1506810>"
+       ]
+      }
+     ],
+     "prompt_number": 8
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "conn.commit()"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 9
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
      "input": [],
      "language": "python",
      "metadata": {},