├── Access Databases Using Jupyter Notebook.pdf.ipynb ├── PyMySQL.PNG ├── SQL Tutorials.ipynb ├── SQLAlchemy.PNG ├── cx_oracle.PNG ├── ipython-sql.PNG ├── ipython-sql1.PNG ├── jupyter-sql.PNG ├── mysql1.PNG ├── mysql2.PNG ├── odbc0.PNG ├── odbc1.PNG ├── odbc2.PNG ├── odbc3.PNG ├── odbc4.PNG ├── odbc5.PNG ├── odbc6.PNG ├── ora_con.PNG ├── oracle1.PNG ├── oracle2.PNG ├── oracle3.PNG ├── oracle4.PNG ├── oracle5.PNG ├── pgAdmin.PNG ├── pgAdmin1.PNG ├── psycopg2.PNG ├── pyodbc.PNG └── sqldb1.PNG /Access Databases Using Jupyter Notebook.pdf.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "zsh:1: no matches found: nbconvert[webpdf]\n", 13 | "Note: you may need to restart the kernel to use updated packages.\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "pip install Pyppeteer" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "\n", 26 | "\n", 27 | "\n", 28 | "
\n", 29 | "

Prepared by Asif Bhat

\n", 30 | " \n", 31 | "

Access Databases using Jupyter Notebook

\n", 32 | "
\n", 33 | "\n", 34 | "" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "# Prerequisites" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "- Install Oracle Database 19c.\n", 49 | "\n", 50 | "\n", 51 | "\n", 52 | "- Install MS SQL Server 2019 Express edition.\n", 53 | "\n", 54 | "\n", 55 | "\n", 56 | "- Install PostgreSQL\n", 57 | "\n", 58 | "\n", 59 | "\n", 60 | "- Install MySQL" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "### Install SQLAlchemy" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "### Install ipython-sql" 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "metadata": {}, 87 | "source": [ 88 | "" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "### Install Pyodbc" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "### Install PyMySQL" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "### Install cx_oracle" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "### Install psycopg2" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 1, 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "import sqlalchemy\n", 154 | "import numpy\n", 155 | "import pandas as pd\n", 156 | "import warnings\n", 157 | "warnings.filterwarnings('ignore')" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "### Connec to SQL Server Database using sqlalchemy" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": {}, 170 | "source": [ 171 | "Sample Database Used \n", 172 | " - https://www.sqlservertutorial.net/sql-server-sample-database/\n", 173 | " \n", 174 | " - https://www.sqlskills.com/resources/conferences/salesdb2014.zip" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 2, 180 | "metadata": {}, 181 | "outputs": [], 182 | "source": [ 183 | "engine_mssql = sqlalchemy.create_engine('mssql+pyodbc://localhost/salesdb?driver=SQL Server?Trusted_Connection=yes')" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 3, 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "name": "stdout", 193 | "output_type": "stream", 194 | "text": [ 195 | "connecting with engine Engine(mssql+pyodbc://localhost/salesdb?driver=SQL+Server)\n" 196 | ] 197 | } 198 | ], 199 | "source": [ 200 | "print(\"connecting with engine \" + str(engine_mssql))" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 4, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "name": "stdout", 210 | "output_type": "stream", 211 | "text": [ 212 | "['Customers', 'Employees', 'Products', 'Sales']\n" 213 | ] 214 | } 215 | ], 216 | "source": [ 217 | "print(engine_mssql.table_names())" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 5, 223 | "metadata": {}, 224 | "outputs": [], 225 | "source": [ 226 | "con_mssql = engine_mssql.connect()" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 6, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/html": [ 237 | "
\n", 238 | "\n", 251 | "\n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | " \n", 267 | " \n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | " \n", 273 | " \n", 274 | " \n", 275 | " \n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | " \n", 280 | " \n", 281 | " \n", 282 | " \n", 283 | " \n", 284 | " \n", 285 | " \n", 286 | " \n", 287 | " \n", 288 | " \n", 289 | " \n", 290 | " \n", 291 | " \n", 292 | " \n", 293 | " \n", 294 | " \n", 295 | " \n", 296 | " \n", 297 | " \n", 298 | " \n", 299 | " \n", 300 | " \n", 301 | " \n", 302 | " \n", 303 | " \n", 304 | " \n", 305 | " \n", 306 | " \n", 307 | " \n", 308 | " \n", 309 | " \n", 310 | " \n", 311 | " \n", 312 | " \n", 313 | " \n", 314 | " \n", 315 | " \n", 316 | " \n", 317 | " \n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " \n", 322 | " \n", 323 | " \n", 324 | " \n", 325 | " \n", 326 | " \n", 327 | " \n", 328 | " \n", 329 | " \n", 330 | " \n", 331 | " \n", 332 | " \n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | " \n", 338 | " \n", 339 | " \n", 340 | " \n", 341 | " \n", 342 | " \n", 343 | " \n", 344 | " \n", 345 | " \n", 346 | " \n", 347 | " \n", 348 | " \n", 349 | " \n", 350 | " \n", 351 | " \n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | " \n", 359 | " \n", 360 | " \n", 361 | " \n", 362 | " \n", 363 | " \n", 364 | " \n", 365 | " \n", 366 | " \n", 367 | " \n", 368 | " \n", 369 | " \n", 370 | " \n", 371 | " \n", 372 | " \n", 373 | " \n", 374 | " \n", 375 | " \n", 376 | " \n", 377 | " \n", 378 | " \n", 379 | " \n", 380 | " \n", 381 | " \n", 382 | " \n", 383 | " \n", 384 | " \n", 385 | " \n", 386 | " \n", 387 | " \n", 388 | " \n", 389 | " \n", 390 | " \n", 391 | " \n", 392 | " \n", 393 | " \n", 394 | " \n", 395 | " \n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | " \n", 402 | " \n", 403 | " \n", 404 | " \n", 405 | " \n", 406 | " \n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | "
EmployeeIDFirstNameMiddleInitialLastName
01AbrahameBennet
12ReginaldlBlotchet-Halls
23CherylaCarson
34MicheleDeFrance
45Innesedel Castillo
56AnnuDull
67MarjorierGreen
78MorningstarrGreene
89BurtrGringlesby
910SheryluHunter
1011LiviaaKarsen
1112CharleneoLocksley
1213StearnsaMacFeather
1314HeathercMcBadden
1415Michael'O'Leary
1516SylviaaPanteley
1617AlbertiRinger
1718AnneiRinger
1819MeandermSmith
1920DeantStraight
2021DirktStringer
2122JohnsonhWhite
2223AkikooYokomoto
\n", 425 | "
" 426 | ], 427 | "text/plain": [ 428 | " EmployeeID FirstName MiddleInitial LastName\n", 429 | "0 1 Abraham e Bennet\n", 430 | "1 2 Reginald l Blotchet-Halls\n", 431 | "2 3 Cheryl a Carson\n", 432 | "3 4 Michel e DeFrance\n", 433 | "4 5 Innes e del Castillo\n", 434 | "5 6 Ann u Dull\n", 435 | "6 7 Marjorie r Green\n", 436 | "7 8 Morningstar r Greene\n", 437 | "8 9 Burt r Gringlesby\n", 438 | "9 10 Sheryl u Hunter\n", 439 | "10 11 Livia a Karsen\n", 440 | "11 12 Charlene o Locksley\n", 441 | "12 13 Stearns a MacFeather\n", 442 | "13 14 Heather c McBadden\n", 443 | "14 15 Michael ' O'Leary\n", 444 | "15 16 Sylvia a Panteley\n", 445 | "16 17 Albert i Ringer\n", 446 | "17 18 Anne i Ringer\n", 447 | "18 19 Meander m Smith\n", 448 | "19 20 Dean t Straight\n", 449 | "20 21 Dirk t Stringer\n", 450 | "21 22 Johnson h White\n", 451 | "22 23 Akiko o Yokomoto" 452 | ] 453 | }, 454 | "execution_count": 6, 455 | "metadata": {}, 456 | "output_type": "execute_result" 457 | } 458 | ], 459 | "source": [ 460 | "query = \"select * from Employees\"\n", 461 | "df_mssql = pd.read_sql_query(query, con_mssql)\n", 462 | "df_mssql" 463 | ] 464 | }, 465 | { 466 | "cell_type": "markdown", 467 | "metadata": {}, 468 | "source": [ 469 | "### Connec to SQL Server Database using ipython-sql" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": {}, 475 | "source": [ 476 | "__To Connect to SQL Server Database using ipython-sql we neeed to first create a odbc connection__" 477 | ] 478 | }, 479 | { 480 | "cell_type": "markdown", 481 | "metadata": {}, 482 | "source": [ 483 | "__Steps to create odbc connection__\n", 484 | "\n", 485 | "1) Go to ODBC Data Source Administration.\n", 486 | "\n", 487 | "2) Click on Add and select the __SQL Serer__ and hit Finish." 488 | ] 489 | }, 490 | { 491 | "cell_type": "markdown", 492 | "metadata": {}, 493 | "source": [ 494 | "" 495 | ] 496 | }, 497 | { 498 | "cell_type": "markdown", 499 | "metadata": {}, 500 | "source": [ 501 | "3) Name your ODBC connection (sqldb).\n", 502 | "\n", 503 | "4) Specify SQL Server name and hit finish." 504 | ] 505 | }, 506 | { 507 | "cell_type": "markdown", 508 | "metadata": {}, 509 | "source": [ 510 | "" 511 | ] 512 | }, 513 | { 514 | "cell_type": "markdown", 515 | "metadata": {}, 516 | "source": [ 517 | "5) Use SQL Server authentication to connect to the database (Salesdb in this case)." 518 | ] 519 | }, 520 | { 521 | "cell_type": "markdown", 522 | "metadata": {}, 523 | "source": [ 524 | "" 525 | ] 526 | }, 527 | { 528 | "cell_type": "markdown", 529 | "metadata": {}, 530 | "source": [ 531 | "6) Select the database (salesdb) and click next." 532 | ] 533 | }, 534 | { 535 | "cell_type": "markdown", 536 | "metadata": {}, 537 | "source": [ 538 | "" 539 | ] 540 | }, 541 | { 542 | "cell_type": "markdown", 543 | "metadata": {}, 544 | "source": [ 545 | "7) Test Data Source and hit finish." 546 | ] 547 | }, 548 | { 549 | "cell_type": "markdown", 550 | "metadata": {}, 551 | "source": [ 552 | "" 553 | ] 554 | }, 555 | { 556 | "cell_type": "markdown", 557 | "metadata": {}, 558 | "source": [ 559 | "8) sqldb data source will be visible in the list now." 560 | ] 561 | }, 562 | { 563 | "cell_type": "markdown", 564 | "metadata": {}, 565 | "source": [ 566 | "" 567 | ] 568 | }, 569 | { 570 | "cell_type": "code", 571 | "execution_count": 7, 572 | "metadata": {}, 573 | "outputs": [ 574 | { 575 | "name": "stdout", 576 | "output_type": "stream", 577 | "text": [ 578 | "(pyodbc.ProgrammingError) ('42000', \"[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'dstran'. Reason: The password of the account has expired. (18487) (SQLDriverConnect); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'dstran'. Reason: The password of the account has expired. (18487)\")\n", 579 | "(Background on this error at: http://sqlalche.me/e/13/f405)\n", 580 | "Connection info needed in SQLAlchemy format, example:\n", 581 | " postgresql://username:password@hostname/dbname\n", 582 | " or an existing connection: dict_keys([])\n" 583 | ] 584 | } 585 | ], 586 | "source": [ 587 | "%load_ext sql\n", 588 | "%sql mssql+pyodbc://dstran:dstran@sqldb" 589 | ] 590 | }, 591 | { 592 | "cell_type": "code", 593 | "execution_count": 8, 594 | "metadata": { 595 | "scrolled": true 596 | }, 597 | "outputs": [ 598 | { 599 | "name": "stdout", 600 | "output_type": "stream", 601 | "text": [ 602 | "Environment variable $DATABASE_URL not set, and no connect string given.\n", 603 | "Connection info needed in SQLAlchemy format, example:\n", 604 | " postgresql://username:password@hostname/dbname\n", 605 | " or an existing connection: dict_keys([])\n" 606 | ] 607 | } 608 | ], 609 | "source": [ 610 | "%%sql\n", 611 | "\n", 612 | "select top 10 * from Employees" 613 | ] 614 | }, 615 | { 616 | "cell_type": "code", 617 | "execution_count": 9, 618 | "metadata": {}, 619 | "outputs": [], 620 | "source": [ 621 | "%reload_ext sql" 622 | ] 623 | }, 624 | { 625 | "cell_type": "code", 626 | "execution_count": 10, 627 | "metadata": {}, 628 | "outputs": [ 629 | { 630 | "name": "stdout", 631 | "output_type": "stream", 632 | "text": [ 633 | "The sql extension is already loaded. To reload it, use:\n", 634 | " %reload_ext sql\n" 635 | ] 636 | } 637 | ], 638 | "source": [ 639 | "%load_ext sql\n", 640 | "%sql mssql+pyodbc://abhat:abhat@sqldb1" 641 | ] 642 | }, 643 | { 644 | "cell_type": "markdown", 645 | "metadata": {}, 646 | "source": [ 647 | "" 648 | ] 649 | }, 650 | { 651 | "cell_type": "code", 652 | "execution_count": 13, 653 | "metadata": {}, 654 | "outputs": [ 655 | { 656 | "name": "stdout", 657 | "output_type": "stream", 658 | "text": [ 659 | " * mssql+pyodbc://abhat:***@sqldb1\n", 660 | " mssql+pyodbc://dstran:***@sqldb\n", 661 | "Done.\n" 662 | ] 663 | }, 664 | { 665 | "data": { 666 | "text/html": [ 667 | "\n", 668 | " \n", 669 | " \n", 670 | " \n", 671 | " \n", 672 | " \n", 673 | " \n", 674 | " \n", 675 | " \n", 676 | " \n", 677 | " \n", 678 | " \n", 679 | " \n", 680 | " \n", 681 | " \n", 682 | " \n", 683 | " \n", 684 | " \n", 685 | " \n", 686 | " \n", 687 | " \n", 688 | " \n", 689 | " \n", 690 | " \n", 691 | " \n", 692 | " \n", 693 | " \n", 694 | " \n", 695 | " \n", 696 | " \n", 697 | " \n", 698 | " \n", 699 | " \n", 700 | " \n", 701 | " \n", 702 | " \n", 703 | " \n", 704 | " \n", 705 | " \n", 706 | " \n", 707 | " \n", 708 | " \n", 709 | " \n", 710 | " \n", 711 | " \n", 712 | " \n", 713 | " \n", 714 | " \n", 715 | " \n", 716 | " \n", 717 | " \n", 718 | " \n", 719 | " \n", 720 | " \n", 721 | " \n", 722 | " \n", 723 | " \n", 724 | " \n", 725 | " \n", 726 | " \n", 727 | " \n", 728 | " \n", 729 | " \n", 730 | " \n", 731 | " \n", 732 | " \n", 733 | " \n", 734 | " \n", 735 | " \n", 736 | " \n", 737 | " \n", 738 | " \n", 739 | " \n", 740 | " \n", 741 | " \n", 742 | " \n", 743 | " \n", 744 | " \n", 745 | " \n", 746 | " \n", 747 | " \n", 748 | " \n", 749 | " \n", 750 | " \n", 751 | " \n", 752 | " \n", 753 | " \n", 754 | " \n", 755 | " \n", 756 | " \n", 757 | " \n", 758 | " \n", 759 | " \n", 760 | " \n", 761 | " \n", 762 | " \n", 763 | " \n", 764 | " \n", 765 | " \n", 766 | " \n", 767 | " \n", 768 | " \n", 769 | " \n", 770 | " \n", 771 | " \n", 772 | " \n", 773 | " \n", 774 | " \n", 775 | " \n", 776 | " \n", 777 | " \n", 778 | " \n", 779 | " \n", 780 | " \n", 781 | " \n", 782 | " \n", 783 | " \n", 784 | " \n", 785 | " \n", 786 | " \n", 787 | " \n", 788 | " \n", 789 | " \n", 790 | " \n", 791 | " \n", 792 | " \n", 793 | " \n", 794 | " \n", 795 | " \n", 796 | " \n", 797 | " \n", 798 | " \n", 799 | " \n", 800 | " \n", 801 | " \n", 802 | " \n", 803 | " \n", 804 | " \n", 805 | " \n", 806 | " \n", 807 | " \n", 808 | " \n", 809 | " \n", 810 | " \n", 811 | "
SpecialOfferIDDescriptionDiscountPctTypeCategoryStartDateEndDateMinQtyMaxQtyrowguidModifiedDate
1No Discount0.0000No DiscountNo Discount2011-05-01 00:00:002014-11-30 00:00:000None0290C4F5-191F-4337-AB6B-0A2DDE03CBF92011-04-01 00:00:00
2Volume Discount 11 to 140.0200Volume DiscountReseller2011-05-31 00:00:002014-05-30 00:00:001114D7542EE7-15DB-4541-985C-5CC27AEF26D62011-05-01 00:00:00
3Volume Discount 15 to 240.0500Volume DiscountReseller2011-05-31 00:00:002014-05-30 00:00:0015244BDBCC01-8CF7-40A9-B643-40EC5B7174912011-05-01 00:00:00
4Volume Discount 25 to 400.1000Volume DiscountReseller2011-05-31 00:00:002014-05-30 00:00:002540504B5E85-8F3F-4EBC-9E1D-C1BC5DEA9AA82011-05-01 00:00:00
5Volume Discount 41 to 600.1500Volume DiscountReseller2011-05-31 00:00:002014-05-30 00:00:004160677E1D9D-944F-4E81-90E8-47EB0A82D48C2011-05-01 00:00:00
6Volume Discount over 600.2000Volume DiscountReseller2011-05-31 00:00:002014-05-30 00:00:0061None8157F569-4E8D-46B6-9347-5D0F726A94392011-05-01 00:00:00
7Mountain-100 Clearance Sale0.3500Discontinued ProductReseller2012-04-13 00:00:002012-05-29 00:00:000None7DF15BF5-6C05-47E7-80A4-22BD1CE59A722012-03-14 00:00:00
8Sport Helmet Discount-20020.1000Seasonal DiscountReseller2012-05-30 00:00:002012-06-29 00:00:000None20C5D2CC-A38F-48F8-AC9A-8F15943E52AE2012-04-30 00:00:00
9Road-650 Overstock0.3000Excess InventoryReseller2012-05-30 00:00:002012-07-30 00:00:000None0CF8472B-F9E6-4945-9E09-549D7DDE21982012-04-30 00:00:00
10Mountain Tire Sale0.5000Excess InventoryCustomer2013-05-14 00:00:002013-07-29 00:00:000None220444AD-2EF3-4E4C-87E9-3AA6EE39A8772013-04-14 00:00:00
" 812 | ], 813 | "text/plain": [ 814 | "[(1, 'No Discount', Decimal('0.0000'), 'No Discount', 'No Discount', datetime.datetime(2011, 5, 1, 0, 0), datetime.datetime(2014, 11, 30, 0, 0), 0, None, '0290C4F5-191F-4337-AB6B-0A2DDE03CBF9', datetime.datetime(2011, 4, 1, 0, 0)),\n", 815 | " (2, 'Volume Discount 11 to 14', Decimal('0.0200'), 'Volume Discount', 'Reseller', datetime.datetime(2011, 5, 31, 0, 0), datetime.datetime(2014, 5, 30, 0, 0), 11, 14, 'D7542EE7-15DB-4541-985C-5CC27AEF26D6', datetime.datetime(2011, 5, 1, 0, 0)),\n", 816 | " (3, 'Volume Discount 15 to 24', Decimal('0.0500'), 'Volume Discount', 'Reseller', datetime.datetime(2011, 5, 31, 0, 0), datetime.datetime(2014, 5, 30, 0, 0), 15, 24, '4BDBCC01-8CF7-40A9-B643-40EC5B717491', datetime.datetime(2011, 5, 1, 0, 0)),\n", 817 | " (4, 'Volume Discount 25 to 40', Decimal('0.1000'), 'Volume Discount', 'Reseller', datetime.datetime(2011, 5, 31, 0, 0), datetime.datetime(2014, 5, 30, 0, 0), 25, 40, '504B5E85-8F3F-4EBC-9E1D-C1BC5DEA9AA8', datetime.datetime(2011, 5, 1, 0, 0)),\n", 818 | " (5, 'Volume Discount 41 to 60', Decimal('0.1500'), 'Volume Discount', 'Reseller', datetime.datetime(2011, 5, 31, 0, 0), datetime.datetime(2014, 5, 30, 0, 0), 41, 60, '677E1D9D-944F-4E81-90E8-47EB0A82D48C', datetime.datetime(2011, 5, 1, 0, 0)),\n", 819 | " (6, 'Volume Discount over 60', Decimal('0.2000'), 'Volume Discount', 'Reseller', datetime.datetime(2011, 5, 31, 0, 0), datetime.datetime(2014, 5, 30, 0, 0), 61, None, '8157F569-4E8D-46B6-9347-5D0F726A9439', datetime.datetime(2011, 5, 1, 0, 0)),\n", 820 | " (7, 'Mountain-100 Clearance Sale', Decimal('0.3500'), 'Discontinued Product', 'Reseller', datetime.datetime(2012, 4, 13, 0, 0), datetime.datetime(2012, 5, 29, 0, 0), 0, None, '7DF15BF5-6C05-47E7-80A4-22BD1CE59A72', datetime.datetime(2012, 3, 14, 0, 0)),\n", 821 | " (8, 'Sport Helmet Discount-2002', Decimal('0.1000'), 'Seasonal Discount', 'Reseller', datetime.datetime(2012, 5, 30, 0, 0), datetime.datetime(2012, 6, 29, 0, 0), 0, None, '20C5D2CC-A38F-48F8-AC9A-8F15943E52AE', datetime.datetime(2012, 4, 30, 0, 0)),\n", 822 | " (9, 'Road-650 Overstock', Decimal('0.3000'), 'Excess Inventory', 'Reseller', datetime.datetime(2012, 5, 30, 0, 0), datetime.datetime(2012, 7, 30, 0, 0), 0, None, '0CF8472B-F9E6-4945-9E09-549D7DDE2198', datetime.datetime(2012, 4, 30, 0, 0)),\n", 823 | " (10, 'Mountain Tire Sale', Decimal('0.5000'), 'Excess Inventory', 'Customer', datetime.datetime(2013, 5, 14, 0, 0), datetime.datetime(2013, 7, 29, 0, 0), 0, None, '220444AD-2EF3-4E4C-87E9-3AA6EE39A877', datetime.datetime(2013, 4, 14, 0, 0))]" 824 | ] 825 | }, 826 | "execution_count": 13, 827 | "metadata": {}, 828 | "output_type": "execute_result" 829 | } 830 | ], 831 | "source": [ 832 | "%%sql\n", 833 | "\n", 834 | "select top 10 * from Sales.SpecialOffer" 835 | ] 836 | }, 837 | { 838 | "cell_type": "markdown", 839 | "metadata": {}, 840 | "source": [ 841 | "### Connect to ORACLE Database using sqlalchemy" 842 | ] 843 | }, 844 | { 845 | "cell_type": "markdown", 846 | "metadata": {}, 847 | "source": [ 848 | "Sample Database Used - https://www.oracletutorial.com/getting-started/oracle-sample-database/" 849 | ] 850 | }, 851 | { 852 | "cell_type": "markdown", 853 | "metadata": {}, 854 | "source": [ 855 | "__This is how my connection looks like in SQL developer :-__" 856 | ] 857 | }, 858 | { 859 | "cell_type": "markdown", 860 | "metadata": {}, 861 | "source": [ 862 | "" 863 | ] 864 | }, 865 | { 866 | "cell_type": "code", 867 | "execution_count": 14, 868 | "metadata": {}, 869 | "outputs": [], 870 | "source": [ 871 | "engine_ora = sqlalchemy.create_engine('oracle://c##abhat:abhat@localhost:1521/orcl')" 872 | ] 873 | }, 874 | { 875 | "cell_type": "code", 876 | "execution_count": 15, 877 | "metadata": {}, 878 | "outputs": [ 879 | { 880 | "name": "stdout", 881 | "output_type": "stream", 882 | "text": [ 883 | "connecting with engine Engine(oracle://c##abhat:***@localhost:1521/orcl)\n" 884 | ] 885 | } 886 | ], 887 | "source": [ 888 | "print(\"connecting with engine \" + str(engine_ora))" 889 | ] 890 | }, 891 | { 892 | "cell_type": "code", 893 | "execution_count": 16, 894 | "metadata": {}, 895 | "outputs": [ 896 | { 897 | "name": "stdout", 898 | "output_type": "stream", 899 | "text": [ 900 | "['regions', 'countries', 'locations', 'warehouses', 'employees', 'product_categories', 'products', 'customers', 'contacts', 'orders', 'order_items', 'inventories']\n" 901 | ] 902 | } 903 | ], 904 | "source": [ 905 | "print(engine_ora.table_names())" 906 | ] 907 | }, 908 | { 909 | "cell_type": "code", 910 | "execution_count": 17, 911 | "metadata": {}, 912 | "outputs": [], 913 | "source": [ 914 | "con_ORA = engine_ora.connect()" 915 | ] 916 | }, 917 | { 918 | "cell_type": "code", 919 | "execution_count": 18, 920 | "metadata": {}, 921 | "outputs": [ 922 | { 923 | "data": { 924 | "text/html": [ 925 | "
\n", 926 | "\n", 939 | "\n", 940 | " \n", 941 | " \n", 942 | " \n", 943 | " \n", 944 | " \n", 945 | " \n", 946 | " \n", 947 | " \n", 948 | " \n", 949 | " \n", 950 | " \n", 951 | " \n", 952 | " \n", 953 | " \n", 954 | " \n", 955 | " \n", 956 | " \n", 957 | " \n", 958 | " \n", 959 | " \n", 960 | " \n", 961 | " \n", 962 | " \n", 963 | " \n", 964 | " \n", 965 | " \n", 966 | " \n", 967 | " \n", 968 | " \n", 969 | " \n", 970 | " \n", 971 | " \n", 972 | " \n", 973 | " \n", 974 | " \n", 975 | " \n", 976 | " \n", 977 | " \n", 978 | " \n", 979 | " \n", 980 | " \n", 981 | " \n", 982 | " \n", 983 | " \n", 984 | " \n", 985 | " \n", 986 | " \n", 987 | " \n", 988 | " \n", 989 | " \n", 990 | " \n", 991 | " \n", 992 | " \n", 993 | " \n", 994 | " \n", 995 | " \n", 996 | " \n", 997 | " \n", 998 | " \n", 999 | " \n", 1000 | " \n", 1001 | " \n", 1002 | " \n", 1003 | " \n", 1004 | " \n", 1005 | " \n", 1006 | " \n", 1007 | " \n", 1008 | " \n", 1009 | " \n", 1010 | " \n", 1011 | " \n", 1012 | " \n", 1013 | " \n", 1014 | " \n", 1015 | " \n", 1016 | " \n", 1017 | " \n", 1018 | " \n", 1019 | " \n", 1020 | " \n", 1021 | " \n", 1022 | " \n", 1023 | " \n", 1024 | " \n", 1025 | " \n", 1026 | " \n", 1027 | " \n", 1028 | " \n", 1029 | " \n", 1030 | " \n", 1031 | " \n", 1032 | " \n", 1033 | " \n", 1034 | " \n", 1035 | " \n", 1036 | " \n", 1037 | " \n", 1038 | " \n", 1039 | " \n", 1040 | " \n", 1041 | " \n", 1042 | " \n", 1043 | " \n", 1044 | " \n", 1045 | " \n", 1046 | " \n", 1047 | " \n", 1048 | " \n", 1049 | " \n", 1050 | " \n", 1051 | " \n", 1052 | "
contact_idfirst_namelast_nameemailphonecustomer_id
0208StephaineBookerstephaine.booker@tsocorp.com+39 55 012 4559208
1209EmilieParsonsemilie.parsons@timewarner.com+39 10 012 4363209
2210JaleesaBowenjaleesa.bowen@cstbrands.com+66 76 012 4633210
3211JeanniePoolejeannie.poole@aboutmcdonalds.com+91 80 012 4637211
4212AdrienneLangadrienne.lang@qualcomm.com+39 2 012 4771212
.....................
314203VellaHancockvella.hancock@pmi.com+39 49 012 4375203
315204RettaMartinezretta.martinez@riteaid.com+39 49 012 4377204
316205AnnelleLawrenceannelle.lawrence@techdata.com+39 10 012 4379205
317206SherronSimonsherron.simon@ally.com+39 10 012 4381206
318207CaritaMcintyrecarita.mcintyre@northwesternmutual.com+86 10 012 4165207
\n", 1053 | "

319 rows × 6 columns

\n", 1054 | "
" 1055 | ], 1056 | "text/plain": [ 1057 | " contact_id first_name last_name email \\\n", 1058 | "0 208 Stephaine Booker stephaine.booker@tsocorp.com \n", 1059 | "1 209 Emilie Parsons emilie.parsons@timewarner.com \n", 1060 | "2 210 Jaleesa Bowen jaleesa.bowen@cstbrands.com \n", 1061 | "3 211 Jeannie Poole jeannie.poole@aboutmcdonalds.com \n", 1062 | "4 212 Adrienne Lang adrienne.lang@qualcomm.com \n", 1063 | ".. ... ... ... ... \n", 1064 | "314 203 Vella Hancock vella.hancock@pmi.com \n", 1065 | "315 204 Retta Martinez retta.martinez@riteaid.com \n", 1066 | "316 205 Annelle Lawrence annelle.lawrence@techdata.com \n", 1067 | "317 206 Sherron Simon sherron.simon@ally.com \n", 1068 | "318 207 Carita Mcintyre carita.mcintyre@northwesternmutual.com \n", 1069 | "\n", 1070 | " phone customer_id \n", 1071 | "0 +39 55 012 4559 208 \n", 1072 | "1 +39 10 012 4363 209 \n", 1073 | "2 +66 76 012 4633 210 \n", 1074 | "3 +91 80 012 4637 211 \n", 1075 | "4 +39 2 012 4771 212 \n", 1076 | ".. ... ... \n", 1077 | "314 +39 49 012 4375 203 \n", 1078 | "315 +39 49 012 4377 204 \n", 1079 | "316 +39 10 012 4379 205 \n", 1080 | "317 +39 10 012 4381 206 \n", 1081 | "318 +86 10 012 4165 207 \n", 1082 | "\n", 1083 | "[319 rows x 6 columns]" 1084 | ] 1085 | }, 1086 | "execution_count": 18, 1087 | "metadata": {}, 1088 | "output_type": "execute_result" 1089 | } 1090 | ], 1091 | "source": [ 1092 | "query = \"select * from CONTACTS\"\n", 1093 | "df_ora = pd.read_sql_query(query, con_ORA)\n", 1094 | "df_ora" 1095 | ] 1096 | }, 1097 | { 1098 | "cell_type": "markdown", 1099 | "metadata": {}, 1100 | "source": [ 1101 | "### Using cx_oracle" 1102 | ] 1103 | }, 1104 | { 1105 | "cell_type": "code", 1106 | "execution_count": 54, 1107 | "metadata": {}, 1108 | "outputs": [], 1109 | "source": [ 1110 | "engine_ora1 = sqlalchemy.create_engine('oracle+cx_oracle://c##abhat:abhat@localhost:1521/orcl')" 1111 | ] 1112 | }, 1113 | { 1114 | "cell_type": "code", 1115 | "execution_count": 55, 1116 | "metadata": {}, 1117 | "outputs": [ 1118 | { 1119 | "name": "stdout", 1120 | "output_type": "stream", 1121 | "text": [ 1122 | "connecting with engine Engine(oracle+cx_oracle://c##abhat:***@localhost:1521/orcl)\n" 1123 | ] 1124 | } 1125 | ], 1126 | "source": [ 1127 | "print(\"connecting with engine \" + str(engine_ora1))" 1128 | ] 1129 | }, 1130 | { 1131 | "cell_type": "code", 1132 | "execution_count": 56, 1133 | "metadata": {}, 1134 | "outputs": [ 1135 | { 1136 | "name": "stdout", 1137 | "output_type": "stream", 1138 | "text": [ 1139 | "['regions', 'countries', 'locations', 'warehouses', 'employees', 'product_categories', 'products', 'customers', 'contacts', 'orders', 'order_items', 'inventories']\n" 1140 | ] 1141 | } 1142 | ], 1143 | "source": [ 1144 | "print(engine_ora1.table_names())" 1145 | ] 1146 | }, 1147 | { 1148 | "cell_type": "code", 1149 | "execution_count": 57, 1150 | "metadata": {}, 1151 | "outputs": [], 1152 | "source": [ 1153 | "con_ORA1 = engine_ora1.connect()" 1154 | ] 1155 | }, 1156 | { 1157 | "cell_type": "code", 1158 | "execution_count": 58, 1159 | "metadata": {}, 1160 | "outputs": [ 1161 | { 1162 | "data": { 1163 | "text/html": [ 1164 | "
\n", 1165 | "\n", 1178 | "\n", 1179 | " \n", 1180 | " \n", 1181 | " \n", 1182 | " \n", 1183 | " \n", 1184 | " \n", 1185 | " \n", 1186 | " \n", 1187 | " \n", 1188 | " \n", 1189 | " \n", 1190 | " \n", 1191 | " \n", 1192 | " \n", 1193 | " \n", 1194 | " \n", 1195 | " \n", 1196 | " \n", 1197 | " \n", 1198 | " \n", 1199 | " \n", 1200 | " \n", 1201 | " \n", 1202 | " \n", 1203 | " \n", 1204 | " \n", 1205 | " \n", 1206 | " \n", 1207 | " \n", 1208 | " \n", 1209 | " \n", 1210 | " \n", 1211 | " \n", 1212 | " \n", 1213 | " \n", 1214 | " \n", 1215 | " \n", 1216 | " \n", 1217 | " \n", 1218 | " \n", 1219 | " \n", 1220 | " \n", 1221 | " \n", 1222 | " \n", 1223 | " \n", 1224 | " \n", 1225 | " \n", 1226 | " \n", 1227 | " \n", 1228 | " \n", 1229 | " \n", 1230 | " \n", 1231 | " \n", 1232 | " \n", 1233 | " \n", 1234 | " \n", 1235 | " \n", 1236 | " \n", 1237 | " \n", 1238 | " \n", 1239 | " \n", 1240 | " \n", 1241 | " \n", 1242 | " \n", 1243 | " \n", 1244 | " \n", 1245 | " \n", 1246 | " \n", 1247 | " \n", 1248 | " \n", 1249 | " \n", 1250 | " \n", 1251 | " \n", 1252 | " \n", 1253 | " \n", 1254 | " \n", 1255 | " \n", 1256 | " \n", 1257 | " \n", 1258 | " \n", 1259 | " \n", 1260 | " \n", 1261 | " \n", 1262 | " \n", 1263 | " \n", 1264 | " \n", 1265 | " \n", 1266 | " \n", 1267 | " \n", 1268 | " \n", 1269 | " \n", 1270 | " \n", 1271 | " \n", 1272 | " \n", 1273 | " \n", 1274 | " \n", 1275 | " \n", 1276 | " \n", 1277 | " \n", 1278 | " \n", 1279 | " \n", 1280 | " \n", 1281 | " \n", 1282 | " \n", 1283 | " \n", 1284 | " \n", 1285 | " \n", 1286 | " \n", 1287 | " \n", 1288 | " \n", 1289 | " \n", 1290 | " \n", 1291 | "
contact_idfirst_namelast_nameemailphonecustomer_id
0208StephaineBookerstephaine.booker@tsocorp.com+39 55 012 4559208
1209EmilieParsonsemilie.parsons@timewarner.com+39 10 012 4363209
2210JaleesaBowenjaleesa.bowen@cstbrands.com+66 76 012 4633210
3211JeanniePoolejeannie.poole@aboutmcdonalds.com+91 80 012 4637211
4212AdrienneLangadrienne.lang@qualcomm.com+39 2 012 4771212
.....................
314203VellaHancockvella.hancock@pmi.com+39 49 012 4375203
315204RettaMartinezretta.martinez@riteaid.com+39 49 012 4377204
316205AnnelleLawrenceannelle.lawrence@techdata.com+39 10 012 4379205
317206SherronSimonsherron.simon@ally.com+39 10 012 4381206
318207CaritaMcintyrecarita.mcintyre@northwesternmutual.com+86 10 012 4165207
\n", 1292 | "

319 rows × 6 columns

\n", 1293 | "
" 1294 | ], 1295 | "text/plain": [ 1296 | " contact_id first_name last_name email \\\n", 1297 | "0 208 Stephaine Booker stephaine.booker@tsocorp.com \n", 1298 | "1 209 Emilie Parsons emilie.parsons@timewarner.com \n", 1299 | "2 210 Jaleesa Bowen jaleesa.bowen@cstbrands.com \n", 1300 | "3 211 Jeannie Poole jeannie.poole@aboutmcdonalds.com \n", 1301 | "4 212 Adrienne Lang adrienne.lang@qualcomm.com \n", 1302 | ".. ... ... ... ... \n", 1303 | "314 203 Vella Hancock vella.hancock@pmi.com \n", 1304 | "315 204 Retta Martinez retta.martinez@riteaid.com \n", 1305 | "316 205 Annelle Lawrence annelle.lawrence@techdata.com \n", 1306 | "317 206 Sherron Simon sherron.simon@ally.com \n", 1307 | "318 207 Carita Mcintyre carita.mcintyre@northwesternmutual.com \n", 1308 | "\n", 1309 | " phone customer_id \n", 1310 | "0 +39 55 012 4559 208 \n", 1311 | "1 +39 10 012 4363 209 \n", 1312 | "2 +66 76 012 4633 210 \n", 1313 | "3 +91 80 012 4637 211 \n", 1314 | "4 +39 2 012 4771 212 \n", 1315 | ".. ... ... \n", 1316 | "314 +39 49 012 4375 203 \n", 1317 | "315 +39 49 012 4377 204 \n", 1318 | "316 +39 10 012 4379 205 \n", 1319 | "317 +39 10 012 4381 206 \n", 1320 | "318 +86 10 012 4165 207 \n", 1321 | "\n", 1322 | "[319 rows x 6 columns]" 1323 | ] 1324 | }, 1325 | "execution_count": 58, 1326 | "metadata": {}, 1327 | "output_type": "execute_result" 1328 | } 1329 | ], 1330 | "source": [ 1331 | "query = \"select * from CONTACTS\"\n", 1332 | "df_ora1 = pd.read_sql_query(query, con_ORA1)\n", 1333 | "df_ora1" 1334 | ] 1335 | }, 1336 | { 1337 | "cell_type": "markdown", 1338 | "metadata": {}, 1339 | "source": [ 1340 | "### Connect to ORACLE Database using ipython-sql" 1341 | ] 1342 | }, 1343 | { 1344 | "cell_type": "code", 1345 | "execution_count": 19, 1346 | "metadata": {}, 1347 | "outputs": [ 1348 | { 1349 | "name": "stdout", 1350 | "output_type": "stream", 1351 | "text": [ 1352 | "The sql extension is already loaded. To reload it, use:\n", 1353 | " %reload_ext sql\n" 1354 | ] 1355 | } 1356 | ], 1357 | "source": [ 1358 | "%load_ext sql\n", 1359 | "%sql \"oracle://c##abhat:abhat@localhost:1521/orcl\"" 1360 | ] 1361 | }, 1362 | { 1363 | "cell_type": "code", 1364 | "execution_count": 20, 1365 | "metadata": {}, 1366 | "outputs": [ 1367 | { 1368 | "name": "stdout", 1369 | "output_type": "stream", 1370 | "text": [ 1371 | " mssql+pyodbc://abhat:***@sqldb1\n", 1372 | " mssql+pyodbc://dstran:***@sqldb\n", 1373 | " * oracle://c##abhat:***@localhost:1521/orcl\n", 1374 | "0 rows affected.\n" 1375 | ] 1376 | }, 1377 | { 1378 | "data": { 1379 | "text/html": [ 1380 | "\n", 1381 | " \n", 1382 | " \n", 1383 | " \n", 1384 | " \n", 1385 | " \n", 1386 | " \n", 1387 | " \n", 1388 | " \n", 1389 | " \n", 1390 | " \n", 1391 | " \n", 1392 | " \n", 1393 | " \n", 1394 | " \n", 1395 | " \n", 1396 | " \n", 1397 | " \n", 1398 | " \n", 1399 | " \n", 1400 | " \n", 1401 | " \n", 1402 | " \n", 1403 | " \n", 1404 | " \n", 1405 | " \n", 1406 | " \n", 1407 | " \n", 1408 | " \n", 1409 | " \n", 1410 | " \n", 1411 | " \n", 1412 | " \n", 1413 | " \n", 1414 | " \n", 1415 | " \n", 1416 | " \n", 1417 | " \n", 1418 | " \n", 1419 | " \n", 1420 | " \n", 1421 | " \n", 1422 | " \n", 1423 | " \n", 1424 | " \n", 1425 | " \n", 1426 | " \n", 1427 | " \n", 1428 | " \n", 1429 | " \n", 1430 | " \n", 1431 | " \n", 1432 | " \n", 1433 | " \n", 1434 | " \n", 1435 | " \n", 1436 | " \n", 1437 | " \n", 1438 | " \n", 1439 | " \n", 1440 | " \n", 1441 | " \n", 1442 | " \n", 1443 | " \n", 1444 | " \n", 1445 | " \n", 1446 | " \n", 1447 | " \n", 1448 | " \n", 1449 | " \n", 1450 | " \n", 1451 | " \n", 1452 | " \n", 1453 | " \n", 1454 | " \n", 1455 | " \n", 1456 | " \n", 1457 | " \n", 1458 | " \n", 1459 | " \n", 1460 | " \n", 1461 | " \n", 1462 | " \n", 1463 | " \n", 1464 | " \n", 1465 | " \n", 1466 | " \n", 1467 | " \n", 1468 | " \n", 1469 | "
contact_idfirst_namelast_nameemailphonecustomer_id
208StephaineBookerstephaine.booker@tsocorp.com+39 55 012 4559208
209EmilieParsonsemilie.parsons@timewarner.com+39 10 012 4363209
210JaleesaBowenjaleesa.bowen@cstbrands.com+66 76 012 4633210
211JeanniePoolejeannie.poole@aboutmcdonalds.com+91 80 012 4637211
212AdrienneLangadrienne.lang@qualcomm.com+39 2 012 4771212
213JessNguyenjess.nguyen@searsholdings.com+39 2 012 4773213
214TandyHousetandy.house@ebay.com+39 2 012 4775214
215HermanStokesherman.stokes@capitalone.com+39 49 012 4777215
216KeeshaLambertkeesha.lambert@emc.com+39 49 012 4779216
217LaurenWilliamsonlauren.williamson@usaa.com+39 49 012 4781217
" 1470 | ], 1471 | "text/plain": [ 1472 | "[(208, 'Stephaine', 'Booker', 'stephaine.booker@tsocorp.com', '+39 55 012 4559', 208),\n", 1473 | " (209, 'Emilie', 'Parsons', 'emilie.parsons@timewarner.com', '+39 10 012 4363', 209),\n", 1474 | " (210, 'Jaleesa', 'Bowen', 'jaleesa.bowen@cstbrands.com', '+66 76 012 4633', 210),\n", 1475 | " (211, 'Jeannie', 'Poole', 'jeannie.poole@aboutmcdonalds.com', '+91 80 012 4637', 211),\n", 1476 | " (212, 'Adrienne', 'Lang', 'adrienne.lang@qualcomm.com', '+39 2 012 4771', 212),\n", 1477 | " (213, 'Jess', 'Nguyen', 'jess.nguyen@searsholdings.com', '+39 2 012 4773', 213),\n", 1478 | " (214, 'Tandy', 'House', 'tandy.house@ebay.com', '+39 2 012 4775', 214),\n", 1479 | " (215, 'Herman', 'Stokes', 'herman.stokes@capitalone.com', '+39 49 012 4777', 215),\n", 1480 | " (216, 'Keesha', 'Lambert', 'keesha.lambert@emc.com', '+39 49 012 4779', 216),\n", 1481 | " (217, 'Lauren', 'Williamson', 'lauren.williamson@usaa.com', '+39 49 012 4781', 217)]" 1482 | ] 1483 | }, 1484 | "execution_count": 20, 1485 | "metadata": {}, 1486 | "output_type": "execute_result" 1487 | } 1488 | ], 1489 | "source": [ 1490 | "%%sql\n", 1491 | "select * from contacts where rownum < 11" 1492 | ] 1493 | }, 1494 | { 1495 | "cell_type": "markdown", 1496 | "metadata": {}, 1497 | "source": [ 1498 | "### Connect to MySQL Database using sqlalchemy" 1499 | ] 1500 | }, 1501 | { 1502 | "cell_type": "markdown", 1503 | "metadata": {}, 1504 | "source": [ 1505 | "__This is how my Schema looks like in MySQL Workbench :-__" 1506 | ] 1507 | }, 1508 | { 1509 | "cell_type": "markdown", 1510 | "metadata": {}, 1511 | "source": [ 1512 | "" 1513 | ] 1514 | }, 1515 | { 1516 | "cell_type": "code", 1517 | "execution_count": 21, 1518 | "metadata": {}, 1519 | "outputs": [], 1520 | "source": [ 1521 | "engine_mysql = sqlalchemy.create_engine('mysql+pymysql://root:root@localhost:3306/sakila')" 1522 | ] 1523 | }, 1524 | { 1525 | "cell_type": "code", 1526 | "execution_count": 22, 1527 | "metadata": {}, 1528 | "outputs": [ 1529 | { 1530 | "name": "stdout", 1531 | "output_type": "stream", 1532 | "text": [ 1533 | "connecting with engine Engine(mysql+pymysql://root:***@localhost:3306/sakila)\n" 1534 | ] 1535 | } 1536 | ], 1537 | "source": [ 1538 | "print(\"connecting with engine \" + str(engine_mysql))" 1539 | ] 1540 | }, 1541 | { 1542 | "cell_type": "code", 1543 | "execution_count": 23, 1544 | "metadata": {}, 1545 | "outputs": [ 1546 | { 1547 | "name": "stdout", 1548 | "output_type": "stream", 1549 | "text": [ 1550 | "['actor', 'address', 'category', 'city', 'country', 'customer', 'film', 'film_actor', 'film_category', 'film_text', 'inventory', 'language', 'payment', 'rental', 'staff', 'store']\n" 1551 | ] 1552 | } 1553 | ], 1554 | "source": [ 1555 | "print(engine_mysql.table_names())" 1556 | ] 1557 | }, 1558 | { 1559 | "cell_type": "code", 1560 | "execution_count": 24, 1561 | "metadata": {}, 1562 | "outputs": [], 1563 | "source": [ 1564 | "con_mysql = engine_mysql.connect()" 1565 | ] 1566 | }, 1567 | { 1568 | "cell_type": "code", 1569 | "execution_count": 25, 1570 | "metadata": {}, 1571 | "outputs": [ 1572 | { 1573 | "data": { 1574 | "text/html": [ 1575 | "
\n", 1576 | "\n", 1589 | "\n", 1590 | " \n", 1591 | " \n", 1592 | " \n", 1593 | " \n", 1594 | " \n", 1595 | " \n", 1596 | " \n", 1597 | " \n", 1598 | " \n", 1599 | " \n", 1600 | " \n", 1601 | " \n", 1602 | " \n", 1603 | " \n", 1604 | " \n", 1605 | " \n", 1606 | " \n", 1607 | " \n", 1608 | " \n", 1609 | " \n", 1610 | " \n", 1611 | " \n", 1612 | " \n", 1613 | " \n", 1614 | " \n", 1615 | " \n", 1616 | " \n", 1617 | " \n", 1618 | " \n", 1619 | " \n", 1620 | " \n", 1621 | " \n", 1622 | " \n", 1623 | " \n", 1624 | " \n", 1625 | " \n", 1626 | " \n", 1627 | " \n", 1628 | " \n", 1629 | " \n", 1630 | " \n", 1631 | " \n", 1632 | " \n", 1633 | " \n", 1634 | " \n", 1635 | " \n", 1636 | " \n", 1637 | " \n", 1638 | " \n", 1639 | " \n", 1640 | " \n", 1641 | " \n", 1642 | " \n", 1643 | " \n", 1644 | " \n", 1645 | " \n", 1646 | " \n", 1647 | " \n", 1648 | " \n", 1649 | " \n", 1650 | " \n", 1651 | " \n", 1652 | " \n", 1653 | " \n", 1654 | " \n", 1655 | " \n", 1656 | " \n", 1657 | " \n", 1658 | " \n", 1659 | " \n", 1660 | " \n", 1661 | " \n", 1662 | " \n", 1663 | " \n", 1664 | " \n", 1665 | " \n", 1666 | " \n", 1667 | " \n", 1668 | " \n", 1669 | " \n", 1670 | " \n", 1671 | " \n", 1672 | " \n", 1673 | " \n", 1674 | " \n", 1675 | " \n", 1676 | " \n", 1677 | " \n", 1678 | " \n", 1679 | " \n", 1680 | " \n", 1681 | " \n", 1682 | " \n", 1683 | " \n", 1684 | " \n", 1685 | " \n", 1686 | " \n", 1687 | " \n", 1688 | " \n", 1689 | " \n", 1690 | " \n", 1691 | " \n", 1692 | " \n", 1693 | " \n", 1694 | " \n", 1695 | " \n", 1696 | "
category_idnamelast_update
01Action2006-02-15 04:46:27
12Animation2006-02-15 04:46:27
23Children2006-02-15 04:46:27
34Classics2006-02-15 04:46:27
45Comedy2006-02-15 04:46:27
56Documentary2006-02-15 04:46:27
67Drama2006-02-15 04:46:27
78Family2006-02-15 04:46:27
89Foreign2006-02-15 04:46:27
910Games2006-02-15 04:46:27
1011Horror2006-02-15 04:46:27
1112Music2006-02-15 04:46:27
1213New2006-02-15 04:46:27
1314Sci-Fi2006-02-15 04:46:27
1415Sports2006-02-15 04:46:27
1516Travel2006-02-15 04:46:27
\n", 1697 | "
" 1698 | ], 1699 | "text/plain": [ 1700 | " category_id name last_update\n", 1701 | "0 1 Action 2006-02-15 04:46:27\n", 1702 | "1 2 Animation 2006-02-15 04:46:27\n", 1703 | "2 3 Children 2006-02-15 04:46:27\n", 1704 | "3 4 Classics 2006-02-15 04:46:27\n", 1705 | "4 5 Comedy 2006-02-15 04:46:27\n", 1706 | "5 6 Documentary 2006-02-15 04:46:27\n", 1707 | "6 7 Drama 2006-02-15 04:46:27\n", 1708 | "7 8 Family 2006-02-15 04:46:27\n", 1709 | "8 9 Foreign 2006-02-15 04:46:27\n", 1710 | "9 10 Games 2006-02-15 04:46:27\n", 1711 | "10 11 Horror 2006-02-15 04:46:27\n", 1712 | "11 12 Music 2006-02-15 04:46:27\n", 1713 | "12 13 New 2006-02-15 04:46:27\n", 1714 | "13 14 Sci-Fi 2006-02-15 04:46:27\n", 1715 | "14 15 Sports 2006-02-15 04:46:27\n", 1716 | "15 16 Travel 2006-02-15 04:46:27" 1717 | ] 1718 | }, 1719 | "execution_count": 25, 1720 | "metadata": {}, 1721 | "output_type": "execute_result" 1722 | } 1723 | ], 1724 | "source": [ 1725 | "query = \"select * from category\"\n", 1726 | "df_mysql = pd.read_sql_query(query, con_mysql)\n", 1727 | "df_mysql" 1728 | ] 1729 | }, 1730 | { 1731 | "cell_type": "markdown", 1732 | "metadata": {}, 1733 | "source": [ 1734 | "### Connect to MySQL Database using ipython-sql" 1735 | ] 1736 | }, 1737 | { 1738 | "cell_type": "markdown", 1739 | "metadata": {}, 1740 | "source": [ 1741 | "" 1742 | ] 1743 | }, 1744 | { 1745 | "cell_type": "code", 1746 | "execution_count": 1, 1747 | "metadata": {}, 1748 | "outputs": [ 1749 | { 1750 | "name": "stdout", 1751 | "output_type": "stream", 1752 | "text": [ 1753 | "Connection info needed in SQLAlchemy format, example:\n", 1754 | " postgresql://username:password@hostname/dbname\n", 1755 | " or an existing connection: dict_keys([])\n", 1756 | "Could not parse rfc1738 URL from string '\"mysql+pymysql://root:root@localhost:3306/world\"'\n", 1757 | "Connection info needed in SQLAlchemy format, example:\n", 1758 | " postgresql://username:password@hostname/dbname\n", 1759 | " or an existing connection: dict_keys([])\n" 1760 | ] 1761 | } 1762 | ], 1763 | "source": [ 1764 | "%load_ext sql\n", 1765 | "%sql \"mysql+pymysql://root:root@localhost:3306/world\"" 1766 | ] 1767 | }, 1768 | { 1769 | "cell_type": "code", 1770 | "execution_count": 27, 1771 | "metadata": {}, 1772 | "outputs": [ 1773 | { 1774 | "name": "stdout", 1775 | "output_type": "stream", 1776 | "text": [ 1777 | " mssql+pyodbc://abhat:***@sqldb1\n", 1778 | " mssql+pyodbc://dstran:***@sqldb\n", 1779 | " * mysql+pymysql://root:***@localhost:3306/world\n", 1780 | " oracle://c##abhat:***@localhost:1521/orcl\n", 1781 | "20 rows affected.\n" 1782 | ] 1783 | }, 1784 | { 1785 | "data": { 1786 | "text/html": [ 1787 | "\n", 1788 | " \n", 1789 | " \n", 1790 | " \n", 1791 | " \n", 1792 | " \n", 1793 | " \n", 1794 | " \n", 1795 | " \n", 1796 | " \n", 1797 | " \n", 1798 | " \n", 1799 | " \n", 1800 | " \n", 1801 | " \n", 1802 | " \n", 1803 | " \n", 1804 | " \n", 1805 | " \n", 1806 | " \n", 1807 | " \n", 1808 | " \n", 1809 | " \n", 1810 | " \n", 1811 | " \n", 1812 | " \n", 1813 | " \n", 1814 | " \n", 1815 | " \n", 1816 | " \n", 1817 | " \n", 1818 | " \n", 1819 | " \n", 1820 | " \n", 1821 | " \n", 1822 | " \n", 1823 | " \n", 1824 | " \n", 1825 | " \n", 1826 | " \n", 1827 | " \n", 1828 | " \n", 1829 | " \n", 1830 | " \n", 1831 | " \n", 1832 | " \n", 1833 | " \n", 1834 | " \n", 1835 | " \n", 1836 | " \n", 1837 | " \n", 1838 | " \n", 1839 | " \n", 1840 | " \n", 1841 | " \n", 1842 | " \n", 1843 | " \n", 1844 | " \n", 1845 | " \n", 1846 | " \n", 1847 | " \n", 1848 | " \n", 1849 | " \n", 1850 | " \n", 1851 | " \n", 1852 | " \n", 1853 | " \n", 1854 | " \n", 1855 | " \n", 1856 | " \n", 1857 | " \n", 1858 | " \n", 1859 | " \n", 1860 | " \n", 1861 | " \n", 1862 | " \n", 1863 | " \n", 1864 | " \n", 1865 | " \n", 1866 | " \n", 1867 | " \n", 1868 | " \n", 1869 | " \n", 1870 | " \n", 1871 | " \n", 1872 | " \n", 1873 | " \n", 1874 | " \n", 1875 | " \n", 1876 | " \n", 1877 | " \n", 1878 | " \n", 1879 | " \n", 1880 | " \n", 1881 | " \n", 1882 | " \n", 1883 | " \n", 1884 | " \n", 1885 | " \n", 1886 | " \n", 1887 | " \n", 1888 | " \n", 1889 | " \n", 1890 | " \n", 1891 | " \n", 1892 | " \n", 1893 | " \n", 1894 | " \n", 1895 | " \n", 1896 | " \n", 1897 | " \n", 1898 | " \n", 1899 | " \n", 1900 | " \n", 1901 | " \n", 1902 | " \n", 1903 | " \n", 1904 | " \n", 1905 | " \n", 1906 | " \n", 1907 | " \n", 1908 | " \n", 1909 | " \n", 1910 | " \n", 1911 | " \n", 1912 | " \n", 1913 | " \n", 1914 | " \n", 1915 | " \n", 1916 | " \n", 1917 | " \n", 1918 | " \n", 1919 | " \n", 1920 | " \n", 1921 | " \n", 1922 | " \n", 1923 | " \n", 1924 | " \n", 1925 | " \n", 1926 | " \n", 1927 | " \n", 1928 | " \n", 1929 | " \n", 1930 | " \n", 1931 | " \n", 1932 | " \n", 1933 | " \n", 1934 | " \n", 1935 | "
IDNameCountryCodeDistrictPopulation
1KabulAFGKabol1780000
2QandaharAFGQandahar237500
3HeratAFGHerat186800
4Mazar-e-SharifAFGBalkh127800
5AmsterdamNLDNoord-Holland731200
6RotterdamNLDZuid-Holland593321
7HaagNLDZuid-Holland440900
8UtrechtNLDUtrecht234323
9EindhovenNLDNoord-Brabant201843
10TilburgNLDNoord-Brabant193238
11GroningenNLDGroningen172701
12BredaNLDNoord-Brabant160398
13ApeldoornNLDGelderland153491
14NijmegenNLDGelderland152463
15EnschedeNLDOverijssel149544
16HaarlemNLDNoord-Holland148772
17AlmereNLDFlevoland142465
18ArnhemNLDGelderland138020
19ZaanstadNLDNoord-Holland135621
20´s-HertogenboschNLDNoord-Brabant129170
" 1936 | ], 1937 | "text/plain": [ 1938 | "[(1, 'Kabul', 'AFG', 'Kabol', 1780000),\n", 1939 | " (2, 'Qandahar', 'AFG', 'Qandahar', 237500),\n", 1940 | " (3, 'Herat', 'AFG', 'Herat', 186800),\n", 1941 | " (4, 'Mazar-e-Sharif', 'AFG', 'Balkh', 127800),\n", 1942 | " (5, 'Amsterdam', 'NLD', 'Noord-Holland', 731200),\n", 1943 | " (6, 'Rotterdam', 'NLD', 'Zuid-Holland', 593321),\n", 1944 | " (7, 'Haag', 'NLD', 'Zuid-Holland', 440900),\n", 1945 | " (8, 'Utrecht', 'NLD', 'Utrecht', 234323),\n", 1946 | " (9, 'Eindhoven', 'NLD', 'Noord-Brabant', 201843),\n", 1947 | " (10, 'Tilburg', 'NLD', 'Noord-Brabant', 193238),\n", 1948 | " (11, 'Groningen', 'NLD', 'Groningen', 172701),\n", 1949 | " (12, 'Breda', 'NLD', 'Noord-Brabant', 160398),\n", 1950 | " (13, 'Apeldoorn', 'NLD', 'Gelderland', 153491),\n", 1951 | " (14, 'Nijmegen', 'NLD', 'Gelderland', 152463),\n", 1952 | " (15, 'Enschede', 'NLD', 'Overijssel', 149544),\n", 1953 | " (16, 'Haarlem', 'NLD', 'Noord-Holland', 148772),\n", 1954 | " (17, 'Almere', 'NLD', 'Flevoland', 142465),\n", 1955 | " (18, 'Arnhem', 'NLD', 'Gelderland', 138020),\n", 1956 | " (19, 'Zaanstad', 'NLD', 'Noord-Holland', 135621),\n", 1957 | " (20, '´s-Hertogenbosch', 'NLD', 'Noord-Brabant', 129170)]" 1958 | ] 1959 | }, 1960 | "execution_count": 27, 1961 | "metadata": {}, 1962 | "output_type": "execute_result" 1963 | } 1964 | ], 1965 | "source": [ 1966 | "%%sql\n", 1967 | "SELECT * FROM city LIMIT 0,20" 1968 | ] 1969 | }, 1970 | { 1971 | "cell_type": "markdown", 1972 | "metadata": {}, 1973 | "source": [ 1974 | "### Connect to PostgreSQL Database using sqlalchemy" 1975 | ] 1976 | }, 1977 | { 1978 | "cell_type": "markdown", 1979 | "metadata": {}, 1980 | "source": [ 1981 | "#### PgAdmin Console" 1982 | ] 1983 | }, 1984 | { 1985 | "cell_type": "markdown", 1986 | "metadata": {}, 1987 | "source": [ 1988 | "" 1989 | ] 1990 | }, 1991 | { 1992 | "cell_type": "code", 1993 | "execution_count": 29, 1994 | "metadata": {}, 1995 | "outputs": [], 1996 | "source": [ 1997 | "engine_postgresql = sqlalchemy.create_engine('postgresql://postgres:postgres@localhost:5432/Pagila')" 1998 | ] 1999 | }, 2000 | { 2001 | "cell_type": "code", 2002 | "execution_count": 30, 2003 | "metadata": {}, 2004 | "outputs": [ 2005 | { 2006 | "name": "stdout", 2007 | "output_type": "stream", 2008 | "text": [ 2009 | "connecting with engine Engine(postgresql://postgres:***@localhost:5432/Pagila)\n" 2010 | ] 2011 | } 2012 | ], 2013 | "source": [ 2014 | "print(\"connecting with engine \" + str(engine_postgresql))" 2015 | ] 2016 | }, 2017 | { 2018 | "cell_type": "code", 2019 | "execution_count": 31, 2020 | "metadata": {}, 2021 | "outputs": [ 2022 | { 2023 | "name": "stdout", 2024 | "output_type": "stream", 2025 | "text": [ 2026 | "['actor', 'film', 'payment_p2007_02', 'payment_p2007_03', 'payment_p2007_04', 'payment_p2007_05', 'payment_p2007_06', 'payment_p2007_01', 'address', 'category', 'city', 'country', 'customer', 'film_actor', 'film_category', 'inventory', 'language', 'rental', 'staff', 'store', 'payment']\n" 2027 | ] 2028 | } 2029 | ], 2030 | "source": [ 2031 | "print(engine_postgresql.table_names())" 2032 | ] 2033 | }, 2034 | { 2035 | "cell_type": "code", 2036 | "execution_count": 32, 2037 | "metadata": {}, 2038 | "outputs": [], 2039 | "source": [ 2040 | "con_postgresql = engine_postgresql.connect()" 2041 | ] 2042 | }, 2043 | { 2044 | "cell_type": "code", 2045 | "execution_count": 33, 2046 | "metadata": {}, 2047 | "outputs": [ 2048 | { 2049 | "data": { 2050 | "text/html": [ 2051 | "
\n", 2052 | "\n", 2065 | "\n", 2066 | " \n", 2067 | " \n", 2068 | " \n", 2069 | " \n", 2070 | " \n", 2071 | " \n", 2072 | " \n", 2073 | " \n", 2074 | " \n", 2075 | " \n", 2076 | " \n", 2077 | " \n", 2078 | " \n", 2079 | " \n", 2080 | " \n", 2081 | " \n", 2082 | " \n", 2083 | " \n", 2084 | " \n", 2085 | " \n", 2086 | " \n", 2087 | " \n", 2088 | " \n", 2089 | " \n", 2090 | " \n", 2091 | " \n", 2092 | " \n", 2093 | " \n", 2094 | " \n", 2095 | " \n", 2096 | " \n", 2097 | " \n", 2098 | " \n", 2099 | " \n", 2100 | " \n", 2101 | " \n", 2102 | " \n", 2103 | " \n", 2104 | " \n", 2105 | " \n", 2106 | " \n", 2107 | " \n", 2108 | " \n", 2109 | " \n", 2110 | " \n", 2111 | " \n", 2112 | " \n", 2113 | " \n", 2114 | " \n", 2115 | " \n", 2116 | " \n", 2117 | " \n", 2118 | " \n", 2119 | " \n", 2120 | " \n", 2121 | " \n", 2122 | " \n", 2123 | " \n", 2124 | " \n", 2125 | " \n", 2126 | " \n", 2127 | " \n", 2128 | " \n", 2129 | " \n", 2130 | " \n", 2131 | " \n", 2132 | " \n", 2133 | " \n", 2134 | " \n", 2135 | " \n", 2136 | " \n", 2137 | " \n", 2138 | " \n", 2139 | " \n", 2140 | " \n", 2141 | " \n", 2142 | " \n", 2143 | " \n", 2144 | " \n", 2145 | " \n", 2146 | " \n", 2147 | " \n", 2148 | " \n", 2149 | " \n", 2150 | " \n", 2151 | " \n", 2152 | " \n", 2153 | " \n", 2154 | " \n", 2155 | " \n", 2156 | " \n", 2157 | " \n", 2158 | " \n", 2159 | " \n", 2160 | " \n", 2161 | " \n", 2162 | " \n", 2163 | " \n", 2164 | " \n", 2165 | " \n", 2166 | " \n", 2167 | " \n", 2168 | " \n", 2169 | " \n", 2170 | " \n", 2171 | " \n", 2172 | "
category_idnamelast_update
01Action2006-02-15 09:46:27
12Animation2006-02-15 09:46:27
23Children2006-02-15 09:46:27
34Classics2006-02-15 09:46:27
45Comedy2006-02-15 09:46:27
56Documentary2006-02-15 09:46:27
67Drama2006-02-15 09:46:27
78Family2006-02-15 09:46:27
89Foreign2006-02-15 09:46:27
910Games2006-02-15 09:46:27
1011Horror2006-02-15 09:46:27
1112Music2006-02-15 09:46:27
1213New2006-02-15 09:46:27
1314Sci-Fi2006-02-15 09:46:27
1415Sports2006-02-15 09:46:27
1516Travel2006-02-15 09:46:27
\n", 2173 | "
" 2174 | ], 2175 | "text/plain": [ 2176 | " category_id name last_update\n", 2177 | "0 1 Action 2006-02-15 09:46:27\n", 2178 | "1 2 Animation 2006-02-15 09:46:27\n", 2179 | "2 3 Children 2006-02-15 09:46:27\n", 2180 | "3 4 Classics 2006-02-15 09:46:27\n", 2181 | "4 5 Comedy 2006-02-15 09:46:27\n", 2182 | "5 6 Documentary 2006-02-15 09:46:27\n", 2183 | "6 7 Drama 2006-02-15 09:46:27\n", 2184 | "7 8 Family 2006-02-15 09:46:27\n", 2185 | "8 9 Foreign 2006-02-15 09:46:27\n", 2186 | "9 10 Games 2006-02-15 09:46:27\n", 2187 | "10 11 Horror 2006-02-15 09:46:27\n", 2188 | "11 12 Music 2006-02-15 09:46:27\n", 2189 | "12 13 New 2006-02-15 09:46:27\n", 2190 | "13 14 Sci-Fi 2006-02-15 09:46:27\n", 2191 | "14 15 Sports 2006-02-15 09:46:27\n", 2192 | "15 16 Travel 2006-02-15 09:46:27" 2193 | ] 2194 | }, 2195 | "execution_count": 33, 2196 | "metadata": {}, 2197 | "output_type": "execute_result" 2198 | } 2199 | ], 2200 | "source": [ 2201 | "query = \"select * from category\"\n", 2202 | "df_postgresql = pd.read_sql_query(query, con_postgresql)\n", 2203 | "df_postgresql" 2204 | ] 2205 | }, 2206 | { 2207 | "cell_type": "markdown", 2208 | "metadata": {}, 2209 | "source": [ 2210 | "### Connect to PostgreSQL Using psycopg2" 2211 | ] 2212 | }, 2213 | { 2214 | "cell_type": "markdown", 2215 | "metadata": {}, 2216 | "source": [ 2217 | "#### pgAdmin Console" 2218 | ] 2219 | }, 2220 | { 2221 | "cell_type": "markdown", 2222 | "metadata": {}, 2223 | "source": [ 2224 | "" 2225 | ] 2226 | }, 2227 | { 2228 | "cell_type": "code", 2229 | "execution_count": 39, 2230 | "metadata": {}, 2231 | "outputs": [], 2232 | "source": [ 2233 | "engine_postgresql1 = sqlalchemy.create_engine('postgresql+psycopg2://postgres:postgres@localhost:5432/NorthWind')" 2234 | ] 2235 | }, 2236 | { 2237 | "cell_type": "code", 2238 | "execution_count": 40, 2239 | "metadata": {}, 2240 | "outputs": [ 2241 | { 2242 | "name": "stdout", 2243 | "output_type": "stream", 2244 | "text": [ 2245 | "connecting with engine Engine(postgresql+psycopg2://postgres:***@localhost:5432/NorthWind)\n" 2246 | ] 2247 | } 2248 | ], 2249 | "source": [ 2250 | "print(\"connecting with engine \" + str(engine_postgresql1))" 2251 | ] 2252 | }, 2253 | { 2254 | "cell_type": "code", 2255 | "execution_count": 41, 2256 | "metadata": {}, 2257 | "outputs": [ 2258 | { 2259 | "name": "stdout", 2260 | "output_type": "stream", 2261 | "text": [ 2262 | "['us_states', 'customers', 'orders', 'employees', 'shippers', 'products', 'order_details', 'categories', 'suppliers', 'region', 'territories', 'employee_territories', 'customer_demographics', 'customer_customer_demo']\n" 2263 | ] 2264 | } 2265 | ], 2266 | "source": [ 2267 | "print(engine_postgresql1.table_names())" 2268 | ] 2269 | }, 2270 | { 2271 | "cell_type": "code", 2272 | "execution_count": 42, 2273 | "metadata": {}, 2274 | "outputs": [], 2275 | "source": [ 2276 | "con_postgresql1 = engine_postgresql1.connect()" 2277 | ] 2278 | }, 2279 | { 2280 | "cell_type": "code", 2281 | "execution_count": 45, 2282 | "metadata": {}, 2283 | "outputs": [ 2284 | { 2285 | "data": { 2286 | "text/html": [ 2287 | "
\n", 2288 | "\n", 2301 | "\n", 2302 | " \n", 2303 | " \n", 2304 | " \n", 2305 | " \n", 2306 | " \n", 2307 | " \n", 2308 | " \n", 2309 | " \n", 2310 | " \n", 2311 | " \n", 2312 | " \n", 2313 | " \n", 2314 | " \n", 2315 | " \n", 2316 | " \n", 2317 | " \n", 2318 | " \n", 2319 | " \n", 2320 | " \n", 2321 | " \n", 2322 | " \n", 2323 | " \n", 2324 | " \n", 2325 | " \n", 2326 | " \n", 2327 | " \n", 2328 | " \n", 2329 | " \n", 2330 | " \n", 2331 | " \n", 2332 | " \n", 2333 | " \n", 2334 | " \n", 2335 | " \n", 2336 | " \n", 2337 | " \n", 2338 | " \n", 2339 | " \n", 2340 | " \n", 2341 | " \n", 2342 | " \n", 2343 | " \n", 2344 | " \n", 2345 | " \n", 2346 | " \n", 2347 | " \n", 2348 | " \n", 2349 | " \n", 2350 | " \n", 2351 | " \n", 2352 | " \n", 2353 | " \n", 2354 | " \n", 2355 | " \n", 2356 | " \n", 2357 | " \n", 2358 | " \n", 2359 | " \n", 2360 | " \n", 2361 | " \n", 2362 | " \n", 2363 | " \n", 2364 | " \n", 2365 | " \n", 2366 | " \n", 2367 | " \n", 2368 | " \n", 2369 | "
category_idcategory_namedescriptionpicture
01BeveragesSoft drinks, coffees, teas, beers, and ales[]
12CondimentsSweet and savory sauces, relishes, spreads, an...[]
23ConfectionsDesserts, candies, and sweet breads[]
34Dairy ProductsCheeses[]
45Grains/CerealsBreads, crackers, pasta, and cereal[]
56Meat/PoultryPrepared meats[]
67ProduceDried fruit and bean curd[]
78SeafoodSeaweed and fish[]
\n", 2370 | "
" 2371 | ], 2372 | "text/plain": [ 2373 | " category_id category_name \\\n", 2374 | "0 1 Beverages \n", 2375 | "1 2 Condiments \n", 2376 | "2 3 Confections \n", 2377 | "3 4 Dairy Products \n", 2378 | "4 5 Grains/Cereals \n", 2379 | "5 6 Meat/Poultry \n", 2380 | "6 7 Produce \n", 2381 | "7 8 Seafood \n", 2382 | "\n", 2383 | " description picture \n", 2384 | "0 Soft drinks, coffees, teas, beers, and ales [] \n", 2385 | "1 Sweet and savory sauces, relishes, spreads, an... [] \n", 2386 | "2 Desserts, candies, and sweet breads [] \n", 2387 | "3 Cheeses [] \n", 2388 | "4 Breads, crackers, pasta, and cereal [] \n", 2389 | "5 Prepared meats [] \n", 2390 | "6 Dried fruit and bean curd [] \n", 2391 | "7 Seaweed and fish [] " 2392 | ] 2393 | }, 2394 | "execution_count": 45, 2395 | "metadata": {}, 2396 | "output_type": "execute_result" 2397 | } 2398 | ], 2399 | "source": [ 2400 | "query = \"select * from categories\"\n", 2401 | "df_postgresql1 = pd.read_sql_query(query, con_postgresql1)\n", 2402 | "df_postgresql1" 2403 | ] 2404 | }, 2405 | { 2406 | "cell_type": "markdown", 2407 | "metadata": {}, 2408 | "source": [ 2409 | "### Connect to PostgreSQL Database using ipython-sql" 2410 | ] 2411 | }, 2412 | { 2413 | "cell_type": "code", 2414 | "execution_count": 48, 2415 | "metadata": {}, 2416 | "outputs": [ 2417 | { 2418 | "name": "stdout", 2419 | "output_type": "stream", 2420 | "text": [ 2421 | "The sql extension is already loaded. To reload it, use:\n", 2422 | " %reload_ext sql\n" 2423 | ] 2424 | } 2425 | ], 2426 | "source": [ 2427 | "%load_ext sql\n", 2428 | "%sql \"postgresql://postgres:postgres@localhost:5432/Pagila\"" 2429 | ] 2430 | }, 2431 | { 2432 | "cell_type": "code", 2433 | "execution_count": 49, 2434 | "metadata": {}, 2435 | "outputs": [ 2436 | { 2437 | "name": "stdout", 2438 | "output_type": "stream", 2439 | "text": [ 2440 | " mssql+pyodbc://abhat:***@sqldb1\n", 2441 | " mssql+pyodbc://dstran:***@sqldb\n", 2442 | " mysql+pymysql://root:***@localhost:3306/world\n", 2443 | " oracle://c##abhat:***@localhost:1521/orcl\n", 2444 | " * postgresql://postgres:***@localhost:5432/Pagila\n", 2445 | "16 rows affected.\n" 2446 | ] 2447 | }, 2448 | { 2449 | "data": { 2450 | "text/html": [ 2451 | "\n", 2452 | " \n", 2453 | " \n", 2454 | " \n", 2455 | " \n", 2456 | " \n", 2457 | " \n", 2458 | " \n", 2459 | " \n", 2460 | " \n", 2461 | " \n", 2462 | " \n", 2463 | " \n", 2464 | " \n", 2465 | " \n", 2466 | " \n", 2467 | " \n", 2468 | " \n", 2469 | " \n", 2470 | " \n", 2471 | " \n", 2472 | " \n", 2473 | " \n", 2474 | " \n", 2475 | " \n", 2476 | " \n", 2477 | " \n", 2478 | " \n", 2479 | " \n", 2480 | " \n", 2481 | " \n", 2482 | " \n", 2483 | " \n", 2484 | " \n", 2485 | " \n", 2486 | " \n", 2487 | " \n", 2488 | " \n", 2489 | " \n", 2490 | " \n", 2491 | " \n", 2492 | " \n", 2493 | " \n", 2494 | " \n", 2495 | " \n", 2496 | " \n", 2497 | " \n", 2498 | " \n", 2499 | " \n", 2500 | " \n", 2501 | " \n", 2502 | " \n", 2503 | " \n", 2504 | " \n", 2505 | " \n", 2506 | " \n", 2507 | " \n", 2508 | " \n", 2509 | " \n", 2510 | " \n", 2511 | " \n", 2512 | " \n", 2513 | " \n", 2514 | " \n", 2515 | " \n", 2516 | " \n", 2517 | " \n", 2518 | " \n", 2519 | " \n", 2520 | " \n", 2521 | " \n", 2522 | " \n", 2523 | " \n", 2524 | " \n", 2525 | " \n", 2526 | " \n", 2527 | " \n", 2528 | " \n", 2529 | " \n", 2530 | " \n", 2531 | " \n", 2532 | " \n", 2533 | " \n", 2534 | " \n", 2535 | " \n", 2536 | " \n", 2537 | "
category_idnamelast_update
1Action2006-02-15 09:46:27
2Animation2006-02-15 09:46:27
3Children2006-02-15 09:46:27
4Classics2006-02-15 09:46:27
5Comedy2006-02-15 09:46:27
6Documentary2006-02-15 09:46:27
7Drama2006-02-15 09:46:27
8Family2006-02-15 09:46:27
9Foreign2006-02-15 09:46:27
10Games2006-02-15 09:46:27
11Horror2006-02-15 09:46:27
12Music2006-02-15 09:46:27
13New2006-02-15 09:46:27
14Sci-Fi2006-02-15 09:46:27
15Sports2006-02-15 09:46:27
16Travel2006-02-15 09:46:27
" 2538 | ], 2539 | "text/plain": [ 2540 | "[(1, 'Action', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2541 | " (2, 'Animation', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2542 | " (3, 'Children', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2543 | " (4, 'Classics', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2544 | " (5, 'Comedy', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2545 | " (6, 'Documentary', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2546 | " (7, 'Drama', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2547 | " (8, 'Family', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2548 | " (9, 'Foreign', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2549 | " (10, 'Games', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2550 | " (11, 'Horror', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2551 | " (12, 'Music', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2552 | " (13, 'New', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2553 | " (14, 'Sci-Fi', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2554 | " (15, 'Sports', datetime.datetime(2006, 2, 15, 9, 46, 27)),\n", 2555 | " (16, 'Travel', datetime.datetime(2006, 2, 15, 9, 46, 27))]" 2556 | ] 2557 | }, 2558 | "execution_count": 49, 2559 | "metadata": {}, 2560 | "output_type": "execute_result" 2561 | } 2562 | ], 2563 | "source": [ 2564 | "%%sql\n", 2565 | "SELECT * FROM category" 2566 | ] 2567 | }, 2568 | { 2569 | "cell_type": "code", 2570 | "execution_count": 50, 2571 | "metadata": {}, 2572 | "outputs": [ 2573 | { 2574 | "name": "stdout", 2575 | "output_type": "stream", 2576 | "text": [ 2577 | "The sql extension is already loaded. To reload it, use:\n", 2578 | " %reload_ext sql\n" 2579 | ] 2580 | } 2581 | ], 2582 | "source": [ 2583 | "%load_ext sql\n", 2584 | "%sql \"postgresql+psycopg2://postgres:postgres@localhost:5432/NorthWind\"" 2585 | ] 2586 | }, 2587 | { 2588 | "cell_type": "code", 2589 | "execution_count": 52, 2590 | "metadata": {}, 2591 | "outputs": [ 2592 | { 2593 | "name": "stdout", 2594 | "output_type": "stream", 2595 | "text": [ 2596 | " mssql+pyodbc://abhat:***@sqldb1\n", 2597 | " mssql+pyodbc://dstran:***@sqldb\n", 2598 | " mysql+pymysql://root:***@localhost:3306/world\n", 2599 | " oracle://c##abhat:***@localhost:1521/orcl\n", 2600 | " * postgresql+psycopg2://postgres:***@localhost:5432/NorthWind\n", 2601 | " postgresql://postgres:***@localhost:5432/Pagila\n", 2602 | "8 rows affected.\n" 2603 | ] 2604 | }, 2605 | { 2606 | "data": { 2607 | "text/html": [ 2608 | "\n", 2609 | " \n", 2610 | " \n", 2611 | " \n", 2612 | " \n", 2613 | " \n", 2614 | " \n", 2615 | " \n", 2616 | " \n", 2617 | " \n", 2618 | " \n", 2619 | " \n", 2620 | " \n", 2621 | " \n", 2622 | " \n", 2623 | " \n", 2624 | " \n", 2625 | " \n", 2626 | " \n", 2627 | " \n", 2628 | " \n", 2629 | " \n", 2630 | " \n", 2631 | " \n", 2632 | " \n", 2633 | " \n", 2634 | " \n", 2635 | " \n", 2636 | " \n", 2637 | " \n", 2638 | " \n", 2639 | " \n", 2640 | " \n", 2641 | " \n", 2642 | " \n", 2643 | " \n", 2644 | " \n", 2645 | "
category_namedescription
BeveragesSoft drinks, coffees, teas, beers, and ales
CondimentsSweet and savory sauces, relishes, spreads, and seasonings
ConfectionsDesserts, candies, and sweet breads
Dairy ProductsCheeses
Grains/CerealsBreads, crackers, pasta, and cereal
Meat/PoultryPrepared meats
ProduceDried fruit and bean curd
SeafoodSeaweed and fish
" 2646 | ], 2647 | "text/plain": [ 2648 | "[('Beverages', 'Soft drinks, coffees, teas, beers, and ales'),\n", 2649 | " ('Condiments', 'Sweet and savory sauces, relishes, spreads, and seasonings'),\n", 2650 | " ('Confections', 'Desserts, candies, and sweet breads'),\n", 2651 | " ('Dairy Products', 'Cheeses'),\n", 2652 | " ('Grains/Cereals', 'Breads, crackers, pasta, and cereal'),\n", 2653 | " ('Meat/Poultry', 'Prepared meats'),\n", 2654 | " ('Produce', 'Dried fruit and bean curd'),\n", 2655 | " ('Seafood', 'Seaweed and fish')]" 2656 | ] 2657 | }, 2658 | "execution_count": 52, 2659 | "metadata": {}, 2660 | "output_type": "execute_result" 2661 | } 2662 | ], 2663 | "source": [ 2664 | "%%sql\n", 2665 | "SELECT category_name , description FROM categories" 2666 | ] 2667 | }, 2668 | { 2669 | "cell_type": "markdown", 2670 | "metadata": {}, 2671 | "source": [ 2672 | "# END" 2673 | ] 2674 | } 2675 | ], 2676 | "metadata": { 2677 | "kernelspec": { 2678 | "display_name": "Python 3 (ipykernel)", 2679 | "language": "python", 2680 | "name": "python3" 2681 | }, 2682 | "language_info": { 2683 | "codemirror_mode": { 2684 | "name": "ipython", 2685 | "version": 3 2686 | }, 2687 | "file_extension": ".py", 2688 | "mimetype": "text/x-python", 2689 | "name": "python", 2690 | "nbconvert_exporter": "python", 2691 | "pygments_lexer": "ipython3", 2692 | "version": "3.9.12" 2693 | }, 2694 | "varInspector": { 2695 | "cols": { 2696 | "lenName": 16, 2697 | "lenType": 16, 2698 | "lenVar": 40 2699 | }, 2700 | "kernels_config": { 2701 | "python": { 2702 | "delete_cmd_postfix": "", 2703 | "delete_cmd_prefix": "del ", 2704 | "library": "var_list.py", 2705 | "varRefreshCmd": "print(var_dic_list())" 2706 | }, 2707 | "r": { 2708 | "delete_cmd_postfix": ") ", 2709 | "delete_cmd_prefix": "rm(", 2710 | "library": "var_list.r", 2711 | "varRefreshCmd": "cat(var_dic_list()) " 2712 | } 2713 | }, 2714 | "types_to_exclude": [ 2715 | "module", 2716 | "function", 2717 | "builtin_function_or_method", 2718 | "instance", 2719 | "_Feature" 2720 | ], 2721 | "window_display": false 2722 | } 2723 | }, 2724 | "nbformat": 4, 2725 | "nbformat_minor": 4 2726 | } 2727 | -------------------------------------------------------------------------------- /PyMySQL.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/PyMySQL.PNG -------------------------------------------------------------------------------- /SQLAlchemy.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/SQLAlchemy.PNG -------------------------------------------------------------------------------- /cx_oracle.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/cx_oracle.PNG -------------------------------------------------------------------------------- /ipython-sql.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/ipython-sql.PNG -------------------------------------------------------------------------------- /ipython-sql1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/ipython-sql1.PNG -------------------------------------------------------------------------------- /jupyter-sql.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/jupyter-sql.PNG -------------------------------------------------------------------------------- /mysql1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/mysql1.PNG -------------------------------------------------------------------------------- /mysql2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/mysql2.PNG -------------------------------------------------------------------------------- /odbc0.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/odbc0.PNG -------------------------------------------------------------------------------- /odbc1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/odbc1.PNG -------------------------------------------------------------------------------- /odbc2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/odbc2.PNG -------------------------------------------------------------------------------- /odbc3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/odbc3.PNG -------------------------------------------------------------------------------- /odbc4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/odbc4.PNG -------------------------------------------------------------------------------- /odbc5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/odbc5.PNG -------------------------------------------------------------------------------- /odbc6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/odbc6.PNG -------------------------------------------------------------------------------- /ora_con.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/ora_con.PNG -------------------------------------------------------------------------------- /oracle1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/oracle1.PNG -------------------------------------------------------------------------------- /oracle2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/oracle2.PNG -------------------------------------------------------------------------------- /oracle3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/oracle3.PNG -------------------------------------------------------------------------------- /oracle4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/oracle4.PNG -------------------------------------------------------------------------------- /oracle5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/oracle5.PNG -------------------------------------------------------------------------------- /pgAdmin.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/pgAdmin.PNG -------------------------------------------------------------------------------- /pgAdmin1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/pgAdmin1.PNG -------------------------------------------------------------------------------- /psycopg2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/psycopg2.PNG -------------------------------------------------------------------------------- /pyodbc.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/pyodbc.PNG -------------------------------------------------------------------------------- /sqldb1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datasciencescoop/SQL/f926847ff05f34a47323eedef3b1db70e1f138fc/sqldb1.PNG --------------------------------------------------------------------------------