diff --git a/lectures/lecture5/notes/codes/databaser.ipynb b/lectures/lecture5/notes/codes/databaser.ipynb index 092ef02de5b38f0a8799781103ca4472d117dac6..32fdbcb91ddc61d806261184133d4dfd4cd7c008 100644 --- a/lectures/lecture5/notes/codes/databaser.ipynb +++ b/lectures/lecture5/notes/codes/databaser.ipynb @@ -164,26 +164,21 @@ " :param regnr: type string\n", " :return: boolean\n", " \"\"\"\n", - " db = get_db_connection()\n", + " with get_db_connection() as db:\n", "\n", - " if not db:\n", - " sys.exit(0)\n", + " cursor = db.cursor()\n", + " sql = \"SELECT count(reg_no) as reg_no_count FROM {0} WHERE reg_no='{1}'\".format(table_name,reg_no)\n", + " cursor.execute(sql)\n", "\n", - " cursor = db.cursor()\n", - " sql = \"SELECT count(reg_no) as reg_no_count FROM {0} WHERE reg_no='{1}'\".format(table_name,reg_no)\n", - " cursor.execute(sql)\n", + " reg_no_count = 0\n", "\n", - " reg_no_count = 0\n", + " for row in cursor:\n", + " reg_no_count= row[0]\n", "\n", - " for row in cursor:\n", - " reg_no_count= row[0]\n", - "\n", - " db.close()\n", - "\n", - " if reg_no_count==0:\n", - " return False\n", - " else:\n", - " return True\n", + " if reg_no_count==0:\n", + " return False\n", + " else:\n", + " return True\n", "\n", "#Lagrer gjenstand i basen\n", "def save_object_db(giver_val,\n",