|
28 | 28 | { |
29 | 29 | "cell_type": "markdown", |
30 | 30 | "metadata": {}, |
31 | | - "source": "# Connect with DataJoint\n\nDataJoint is the primary way to connect to the database in this book. The DataJoint client library reads the database credentials from the environment variables `DJ_HOST`, `DJ_USER`, and `DJ_PASS`. \n\nSimply importing the DataJoint library is sufficient—it will connect to the database automatically when needed. Here we call `dj.conn()` only to verify the connection, but this step is not required in normal use." |
| 31 | + "source": [ |
| 32 | + "# Connect with DataJoint\n", |
| 33 | + "\n", |
| 34 | + "DataJoint is the primary way to connect to the database in this book. The DataJoint client library reads the database credentials from the environment variables `DJ_HOST`, `DJ_USER`, and `DJ_PASS`. \n", |
| 35 | + "\n", |
| 36 | + "Simply importing the DataJoint library is sufficient—it will connect to the database automatically when needed. Here we call `dj.conn()` only to verify the connection, but this step is not required in normal use." |
| 37 | + ] |
32 | 38 | }, |
33 | 39 | { |
34 | 40 | "cell_type": "code", |
35 | 41 | "execution_count": null, |
36 | 42 | "metadata": {}, |
37 | 43 | "outputs": [], |
38 | | - "source": "import datajoint as dj\ndj.conn() # test the connection (optional)" |
| 44 | + "source": [ |
| 45 | + "import datajoint as dj\n", |
| 46 | + "dj.conn() # test the connection (optional)" |
| 47 | + ] |
39 | 48 | }, |
40 | 49 | { |
41 | 50 | "cell_type": "markdown", |
42 | 51 | "metadata": {}, |
43 | | - "source": "# Connect with SQL Magic\n\nSQL \"Jupyter magic\" allows executing SQL statements directly in Jupyter notebooks, implemented by the [`jupysql`](https://ploomber.io/blog/jupysql/) library. This is useful for quick interactive SQL queries and for learning SQL syntax. We will use SQL magic in this book for demonstrating SQL concepts, but it is not used as part of Python application code.\n\nThe following cell sets up the SQL magic connection to the database." |
| 52 | + "source": [ |
| 53 | + "# Connect with SQL Magic\n", |
| 54 | + "\n", |
| 55 | + "SQL \"Jupyter magic\" allows executing SQL statements directly in Jupyter notebooks, implemented by the [`jupysql`](https://ploomber.io/blog/jupysql/) library. This is useful for quick interactive SQL queries and for learning SQL syntax. We will use SQL magic in this book for demonstrating SQL concepts, but it is not used as part of Python application code.\n", |
| 56 | + "\n", |
| 57 | + "The following cell sets up the SQL magic connection to the database." |
| 58 | + ] |
44 | 59 | }, |
45 | 60 | { |
46 | 61 | "cell_type": "code", |
|
51 | 66 | } |
52 | 67 | }, |
53 | 68 | "outputs": [], |
54 | | - "source": "%load_ext sql\n%sql mysql+pymysql://dev:devpass@db" |
| 69 | + "source": [ |
| 70 | + "%load_ext sql\n", |
| 71 | + "%sql mysql+pymysql://dev:devpass@db" |
| 72 | + ] |
55 | 73 | }, |
56 | 74 | { |
57 | 75 | "cell_type": "markdown", |
58 | 76 | "metadata": {}, |
59 | | - "source": "You can issue SQL commands from a Jupyter cell by starting it with `%%sql`.\nChange the cell type to `SQL` for appropriate syntax highlighting." |
| 77 | + "source": [ |
| 78 | + "You can issue SQL commands from a Jupyter cell by starting it with `%%sql`.\n", |
| 79 | + "Change the cell type to `SQL` for appropriate syntax highlighting." |
| 80 | + ] |
60 | 81 | }, |
61 | 82 | { |
62 | 83 | "cell_type": "code", |
63 | 84 | "execution_count": null, |
64 | 85 | "metadata": {}, |
65 | 86 | "outputs": [], |
66 | | - "source": "%%sql\n-- show all users\nSELECT User FROM mysql.user" |
| 87 | + "source": [ |
| 88 | + "%%sql\n", |
| 89 | + "-- show all users\n", |
| 90 | + "SELECT User FROM mysql.user" |
| 91 | + ] |
67 | 92 | }, |
68 | 93 | { |
69 | 94 | "cell_type": "markdown", |
70 | 95 | "metadata": {}, |
71 | | - "source": "# Connect with a Python MySQL Client\n\nTo issue SQL queries directly from Python code (outside of Jupyter magic), you can use a conventional SQL client library such as `pymysql`. This approach gives you full programmatic control over database interactions and is useful when you need to execute raw SQL within Python scripts." |
| 96 | + "source": [ |
| 97 | + "# Connect with a Python MySQL Client\n", |
| 98 | + "\n", |
| 99 | + "To issue SQL queries directly from Python code (outside of Jupyter magic), you can use a conventional SQL client library such as `pymysql`. This approach gives you full programmatic control over database interactions and is useful when you need to execute raw SQL within Python scripts." |
| 100 | + ] |
72 | 101 | }, |
73 | 102 | { |
74 | 103 | "cell_type": "code", |
75 | 104 | "execution_count": null, |
76 | 105 | "metadata": {}, |
77 | 106 | "outputs": [], |
78 | | - "source": "import os\nimport pymysql\n\n# create a database connection\nconn = pymysql.connect(\n host=os.environ['DJ_HOST'], \n user=os.environ['DJ_USER'], \n password=os.environ['DJ_PASS']\n)" |
| 107 | + "source": [ |
| 108 | + "import os\n", |
| 109 | + "import pymysql\n", |
| 110 | + "\n", |
| 111 | + "# create a database connection\n", |
| 112 | + "conn = pymysql.connect(\n", |
| 113 | + " host=os.environ['DJ_HOST'], \n", |
| 114 | + " user=os.environ['DJ_USER'], \n", |
| 115 | + " password=os.environ['DJ_PASS']\n", |
| 116 | + ")" |
| 117 | + ] |
79 | 118 | }, |
80 | 119 | { |
81 | 120 | "cell_type": "code", |
82 | 121 | "execution_count": null, |
83 | 122 | "metadata": {}, |
84 | 123 | "outputs": [], |
85 | | - "source": "# create a query cursor and issue an SQL query\ncur = conn.cursor()\ncur.execute('SELECT User FROM mysql.user')\ncur.fetchall()" |
| 124 | + "source": [ |
| 125 | + "# create a query cursor and issue an SQL query\n", |
| 126 | + "cur = conn.cursor()\n", |
| 127 | + "cur.execute('SELECT User FROM mysql.user')\n", |
| 128 | + "cur.fetchall()" |
| 129 | + ] |
86 | 130 | }, |
87 | 131 | { |
88 | 132 | "cell_type": "markdown", |
89 | 133 | "metadata": {}, |
90 | | - "source": "We are all set for executing all the database queries in this book!" |
| 134 | + "source": [ |
| 135 | + "We are all set for executing all the database queries in this book!" |
| 136 | + ] |
91 | 137 | } |
92 | 138 | ], |
93 | 139 | "metadata": { |
|
0 commit comments