|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import psycopg2\n", |
| 10 | + "import os\n", |
| 11 | + "\n", |
| 12 | + "from sqlalchemy import create_engine\n", |
| 13 | + "from sqlalchemy import Column, String, Integer\n", |
| 14 | + "from sqlalchemy.ext.declarative import declarative_base\n", |
| 15 | + "from sqlalchemy.orm import sessionmaker" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "markdown", |
| 20 | + "metadata": {}, |
| 21 | + "source": [ |
| 22 | + "# Connect to DB with SQL Alchemy" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 5, |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "USER = os.environ['POSTGRE_USERNAME']\n", |
| 32 | + "PASSWORD = os.environ['POSTGRE_PASSWORD']" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": 17, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [ |
| 40 | + { |
| 41 | + "data": { |
| 42 | + "text/plain": [ |
| 43 | + "('postgres', 'admin')" |
| 44 | + ] |
| 45 | + }, |
| 46 | + "execution_count": 17, |
| 47 | + "metadata": {}, |
| 48 | + "output_type": "execute_result" |
| 49 | + } |
| 50 | + ], |
| 51 | + "source": [ |
| 52 | + "USER, PASSWORD" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "#### create engine" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": 44, |
| 65 | + "metadata": {}, |
| 66 | + "outputs": [], |
| 67 | + "source": [ |
| 68 | + "db_conn_string = 'postgresql+psycopg2://'+USER+':'+PASSWORD+'@localhost/db_Advanced_SQL_Application_Development'\n", |
| 69 | + "engine = create_engine(db_conn_string)" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "markdown", |
| 74 | + "metadata": {}, |
| 75 | + "source": [ |
| 76 | + "#### create session" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": 45, |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "Session = sessionmaker(engine)\n", |
| 86 | + "session = Session()" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "# Creating Classes" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "code", |
| 98 | + "execution_count": 46, |
| 99 | + "metadata": {}, |
| 100 | + "outputs": [], |
| 101 | + "source": [ |
| 102 | + "# create base class\n", |
| 103 | + "base = declarative_base()" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": 47, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "# product class\n", |
| 113 | + "class Product(base):\n", |
| 114 | + " __tablename__ = 'products'\n", |
| 115 | + " product_id = Column(Integer, primary_key=True)\n", |
| 116 | + " product_name = Column(String)\n", |
| 117 | + " product_type = Column(String)" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "markdown", |
| 122 | + "metadata": {}, |
| 123 | + "source": [ |
| 124 | + "----------" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "markdown", |
| 129 | + "metadata": {}, |
| 130 | + "source": [ |
| 131 | + "# Query products using Product class" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 48, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "products = session.query(Product)" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": 49, |
| 146 | + "metadata": {}, |
| 147 | + "outputs": [ |
| 148 | + { |
| 149 | + "data": { |
| 150 | + "text/plain": [ |
| 151 | + "<sqlalchemy.orm.query.Query at 0xfdc8b5fdc0>" |
| 152 | + ] |
| 153 | + }, |
| 154 | + "execution_count": 49, |
| 155 | + "metadata": {}, |
| 156 | + "output_type": "execute_result" |
| 157 | + } |
| 158 | + ], |
| 159 | + "source": [ |
| 160 | + "products" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": 50, |
| 166 | + "metadata": {}, |
| 167 | + "outputs": [ |
| 168 | + { |
| 169 | + "name": "stdout", |
| 170 | + "output_type": "stream", |
| 171 | + "text": [ |
| 172 | + "Reflector oven\n", |
| 173 | + "Convection microwave\n", |
| 174 | + "Pressure fryer\n", |
| 175 | + "Multicooker\n", |
| 176 | + "Food steamer\n", |
| 177 | + "Chapati maker\n", |
| 178 | + "Mess kit\n", |
| 179 | + "Rotisserie\n", |
| 180 | + "Sous-vide cooker\n", |
| 181 | + "Rocket mass heater\n", |
| 182 | + "Cheesemelter\n", |
| 183 | + "Hot plate\n", |
| 184 | + "Flattop grill\n", |
| 185 | + "Wet grinder\n", |
| 186 | + "Masonry oven\n", |
| 187 | + "Chocolatera\n", |
| 188 | + "Turkey fryer\n", |
| 189 | + "Bread machine\n", |
| 190 | + "Roasting jack\n", |
| 191 | + "Brasero (heater)\n", |
| 192 | + "Susceptor\n", |
| 193 | + "Slow cooker\n", |
| 194 | + "Butane torch\n", |
| 195 | + "Microwave oven\n", |
| 196 | + "Solar cooker\n", |
| 197 | + "Deep fryer\n", |
| 198 | + "Popcorn maker\n", |
| 199 | + "Russian oven\n", |
| 200 | + "Clome oven\n", |
| 201 | + "Convection oven\n", |
| 202 | + "Beehive oven\n", |
| 203 | + "Toaster and toaster ovens\n", |
| 204 | + "Field kitchen\n", |
| 205 | + "Corn roaster\n", |
| 206 | + "Self-cleaning oven\n", |
| 207 | + "Wood-fired oven\n", |
| 208 | + "Kitchener range\n", |
| 209 | + "Rice polisher\n", |
| 210 | + "Soy milk maker\n", |
| 211 | + "Crepe maker\n", |
| 212 | + "Oven\n", |
| 213 | + "Hot box (appliance)\n", |
| 214 | + "Combi steamer\n", |
| 215 | + "Rice cooker\n", |
| 216 | + "Fire pot\n", |
| 217 | + "Salamander broiler\n", |
| 218 | + "Vacuum fryer\n", |
| 219 | + "Fufu Machine\n", |
| 220 | + "Tabun oven\n", |
| 221 | + "Pancake machine\n", |
| 222 | + "Barbecue grill\n", |
| 223 | + "Panini sandwich grill\n", |
| 224 | + "Air fryer\n", |
| 225 | + "Chorkor oven\n", |
| 226 | + "Communal oven\n", |
| 227 | + "Pressure cooker\n", |
| 228 | + "Halogen oven\n", |
| 229 | + "Instant Pot\n", |
| 230 | + "Waffle iron\n", |
| 231 | + "Stove\n", |
| 232 | + "Earth oven\n", |
| 233 | + "Electric cooker\n", |
| 234 | + "Espresso machine\n", |
| 235 | + "Coffee pot\n" |
| 236 | + ] |
| 237 | + } |
| 238 | + ], |
| 239 | + "source": [ |
| 240 | + "for product in products:\n", |
| 241 | + " print(product.product_name)" |
| 242 | + ] |
| 243 | + }, |
| 244 | + { |
| 245 | + "cell_type": "markdown", |
| 246 | + "metadata": {}, |
| 247 | + "source": [ |
| 248 | + "------" |
| 249 | + ] |
| 250 | + }, |
| 251 | + { |
| 252 | + "cell_type": "markdown", |
| 253 | + "metadata": {}, |
| 254 | + "source": [ |
| 255 | + "# Search for specific product\n", |
| 256 | + "- filter" |
| 257 | + ] |
| 258 | + }, |
| 259 | + { |
| 260 | + "cell_type": "code", |
| 261 | + "execution_count": 52, |
| 262 | + "metadata": {}, |
| 263 | + "outputs": [], |
| 264 | + "source": [ |
| 265 | + "products = session.query(Product).filter(Product.product_type=='fryer')" |
| 266 | + ] |
| 267 | + }, |
| 268 | + { |
| 269 | + "cell_type": "code", |
| 270 | + "execution_count": 53, |
| 271 | + "metadata": {}, |
| 272 | + "outputs": [ |
| 273 | + { |
| 274 | + "name": "stdout", |
| 275 | + "output_type": "stream", |
| 276 | + "text": [ |
| 277 | + "Pressure fryer\n", |
| 278 | + "Turkey fryer\n", |
| 279 | + "Deep fryer\n", |
| 280 | + "Vacuum fryer\n", |
| 281 | + "Air fryer\n" |
| 282 | + ] |
| 283 | + } |
| 284 | + ], |
| 285 | + "source": [ |
| 286 | + "for product in products:\n", |
| 287 | + " print(product.product_name)" |
| 288 | + ] |
| 289 | + }, |
| 290 | + { |
| 291 | + "cell_type": "markdown", |
| 292 | + "metadata": {}, |
| 293 | + "source": [ |
| 294 | + "----------\n", |
| 295 | + "----------" |
| 296 | + ] |
| 297 | + }, |
| 298 | + { |
| 299 | + "cell_type": "markdown", |
| 300 | + "metadata": {}, |
| 301 | + "source": [ |
| 302 | + "# Challenge\n", |
| 303 | + "define a class using SQLalchemy that corresponds to a supplier table which has four column:\n", |
| 304 | + "\n", |
| 305 | + "- supplier_id (integer)\n", |
| 306 | + "- supplier_name(variable character string)\n", |
| 307 | + "- supplier_region(varaible character string)\n", |
| 308 | + "- supplier_level (integer)" |
| 309 | + ] |
| 310 | + }, |
| 311 | + { |
| 312 | + "cell_type": "code", |
| 313 | + "execution_count": null, |
| 314 | + "metadata": {}, |
| 315 | + "outputs": [], |
| 316 | + "source": [ |
| 317 | + "class Supplier(base):\n", |
| 318 | + " __tablename__ = 'suppliers'\n", |
| 319 | + " supplier_id = Column(Integer, primary_key=True)\n", |
| 320 | + " supplier_name = Column(String)\n", |
| 321 | + " supplier_region = Column(String)\n", |
| 322 | + " supplier_level = Column(Integer)" |
| 323 | + ] |
| 324 | + }, |
| 325 | + { |
| 326 | + "cell_type": "code", |
| 327 | + "execution_count": null, |
| 328 | + "metadata": {}, |
| 329 | + "outputs": [], |
| 330 | + "source": [] |
| 331 | + } |
| 332 | + ], |
| 333 | + "metadata": { |
| 334 | + "kernelspec": { |
| 335 | + "display_name": "Python 3", |
| 336 | + "language": "python", |
| 337 | + "name": "python3" |
| 338 | + }, |
| 339 | + "language_info": { |
| 340 | + "codemirror_mode": { |
| 341 | + "name": "ipython", |
| 342 | + "version": 3 |
| 343 | + }, |
| 344 | + "file_extension": ".py", |
| 345 | + "mimetype": "text/x-python", |
| 346 | + "name": "python", |
| 347 | + "nbconvert_exporter": "python", |
| 348 | + "pygments_lexer": "ipython3", |
| 349 | + "version": "3.8.5" |
| 350 | + } |
| 351 | + }, |
| 352 | + "nbformat": 4, |
| 353 | + "nbformat_minor": 4 |
| 354 | +} |
0 commit comments