{"id":5919,"date":"2024-11-29T17:12:23","date_gmt":"2024-11-29T08:12:23","guid":{"rendered":"https:\/\/blog.since2020.jp\/?p=5919"},"modified":"2024-11-29T17:12:43","modified_gmt":"2024-11-29T08:12:43","slug":"jinja-python-sql","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/jinja-python-sql\/","title":{"rendered":"Jinja\u3068Python\u3067\u5b66\u3076SQL\u306e\u52d5\u7684\u751f\u6210"},"content":{"rendered":"\n<p>Jinja\u306fPython\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30a8\u30f3\u30b8\u30f3\u3067\u3001\u52d5\u7684\u306bSQL\u30af\u30a8\u30ea\u3092\u751f\u6210\u3067\u304d\u308b\u4fbf\u5229\u306a\u30c4\u30fc\u30eb\u3067\u3059\u3002\u8907\u96d1\u306a\u6761\u4ef6\u3084\u7e70\u308a\u8fd4\u3057\u51e6\u7406\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u8a18\u8ff0\u3067\u304d\u308b\u305f\u3081\u3001SQL\u306e\u4f5c\u6210\u3084\u30c7\u30fc\u30bf\u5206\u6790\u306e\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002<\/p>\n\n\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">Jinja\u306fPython\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30a8\u30f3\u30b8\u30f3\u3067\u3001\u52d5\u7684\u306bSQL\u30af\u30a8\u30ea\u3092\u751f\u6210\u3067\u304d\u308b\u4fbf\u5229\u306a\u30c4\u30fc\u30eb\u3067\u3059\u3002\u8907\u96d1\u306a\u6761\u4ef6\u3084\u7e70\u308a\u8fd4\u3057\u51e6\u7406\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u8a18\u8ff0\u3067\u304d\u308b\u305f\u3081\u3001SQL\u306e\u4f5c\u6210\u3084\u30c7\u30fc\u30bf\u5206\u6790\u306e\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002<\/span><\/p>\r\n<p><!-- notionvc: f59ac362-beb2-4f45-bd52-d32ad8651bd1 --><\/p>\n\n<h2>\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">Google Colab\u307e\u305f\u306f\u4efb\u610f\u306ePython\u74b0\u5883\u3067\u4ee5\u4e0b\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/span><\/p>\r\n<pre><span style=\"font-family: arial, helvetica, sans-serif\"><code class=\"language-python\"><\/code><\/span><\/pre>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><span style=\"font-family: arial, helvetica, sans-serif\"><code>!pip install Jinja2 pandas<\/code><\/span><\/pre>\r\n<\/div>\r\n<pre><span style=\"font-family: arial, helvetica, sans-serif\"><code class=\"language-python\"><\/code><\/span><\/pre>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u3053\u306e\u4f8b\u3067\u306fSQLite\u3092\u4f7f\u7528\u3057\u3066\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u3001Jinja\u3092\u4f7f\u3063\u305fSQL\u30af\u30a8\u30ea\u3092\u52d5\u7684\u306b\u751f\u6210\u3057\u307e\u3059\u3002<\/span><\/p>\r\n<p><!-- notionvc: 6c615d28-1503-4c97-b6c3-a586c66ce066 --><\/p>\n\n<h2>\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\u306e\u4f5c\u6210<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3001SQLite\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/span><!-- notionvc: 221865a8-1b14-49c6-acbf-689b5f45877c --><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import sqlite3\r\nimport pandas as pd\r\n\r\n# SQLite\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u4f5c\u6210\r\nconn = sqlite3.connect(\":memory:\")\r\n\r\n# \u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\r\nsales_data = pd.DataFrame({\r\n\"sale_id\": [1, 2, 3],\r\n\"customer_id\": [101, 102, 101],\r\n\"sale_date\": [\"2024-01-01\", \"2024-01-02\", \"2024-01-03\"],\r\n\"amount\": [100, 200, 150],\r\n})\r\n\r\ncustomer_orders = pd.DataFrame({\r\n\"customer_id\": [1, 1, 2],\r\n\"order_id\": [1001, 1002, 1003],\r\n\"order_date\": [\"2024-01-01\", \"2024-01-02\", \"2024-01-02\"],\r\n\"total_amount\": [500, 300, 700],\r\n})\r\n\r\nproduct_sales = pd.DataFrame({\r\n\"product_id\": [\"A\", \"B\"],\r\n\"region\": [\"East\", \"West\"],\r\n\"sales_q1\": [500, 300],\r\n\"sales_q2\": [600, 400],\r\n\"sales_q3\": [700, 500],\r\n\"sales_q4\": [800, 600],\r\n})\r\n\r\n# \u30c7\u30fc\u30bf\u3092SQLite\u306b\u633f\u5165\r\nsales_data.to_sql(\"sales_data\", conn, index=False, if_exists=\"replace\")\r\ncustomer_orders.to_sql(\"customer_orders\", conn, index=False, if_exists=\"replace\")\r\nproduct_sales.to_sql(\"product_sales\", conn, index=False, if_exists=\"replace\")\r\n\r\n# \u5404\u30c6\u30fc\u30d6\u30eb\u306e\u5185\u5bb9\u3092\u78ba\u8a8d\r\nprint(\"=== sales_data ===\")\r\nprint(pd.read_sql(\"SELECT * FROM sales_data\", conn))\r\n\r\nprint(\"\\\\n=== customer_orders ===\")\r\nprint(pd.read_sql(\"SELECT * FROM customer_orders\", conn))\r\n\r\nprint(\"\\\\n=== product_sales ===\")\r\nprint(pd.read_sql(\"SELECT * FROM product_sales\", conn))<\/code><\/pre>\r\n<\/div>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-4-1.png\" alt=\"\" width=\"1470\" height=\"576\" class=\"alignnone size-full wp-image-5920\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-4-1.png 1470w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-4-1-300x118.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-4-1-1024x401.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-4-1-768x301.png 768w\" sizes=\"(max-width: 1470px) 100vw, 1470px\" \/><\/p>\r\n<p>&nbsp;<\/p>\n\n<h2>1. Jinja\u306e\u57fa\u672c\u69cb\u6587<\/h2>\n<b><span style=\"font-family: arial, helvetica, sans-serif\"><strong>1.1 \u30c6\u30fc\u30d6\u30eb\u53c2\u7167<\/strong><\/span><\/b>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">Jinja\u3092\u4f7f\u3063\u3066\u52d5\u7684\u306b\u30c6\u30fc\u30d6\u30eb\u3092\u53c2\u7167\u3057\u307e\u3059\u3002<\/span><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><span style=\"font-family: arial, helvetica, sans-serif\"><code>from jinja2 import Template\r\n\r\n# \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u5b9a\u7fa9\r\nsql_template = \"\"\"\r\nSELECT *\r\nFROM {{ table_name }}\r\n\"\"\"\r\n\r\n# \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\r\ntemplate = Template(sql_template)\r\nrendered_sql = template.render(table_name=\"sales_data\")\r\nprint(rendered_sql)\r\n\r\n# \u5b9f\u884c\r\npd.read_sql(rendered_sql, conn)\r\n<\/code><\/span><\/pre>\r\n<\/div>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-5-1.png\" alt=\"\" width=\"760\" height=\"340\" class=\"alignnone size-full wp-image-5921\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-5-1.png 760w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-5-1-300x134.png 300w\" sizes=\"(max-width: 760px) 100vw, 760px\" \/><\/span><\/p>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><strong>\u89e3\u8aac:<\/strong><\/span><\/p>\r\n<ul>\r\n\t<li><span style=\"font-family: arial, helvetica, sans-serif\"><code>{{ table_name }}<\/code> \u306e\u90e8\u5206\u306b\u5909\u6570 <code>table_name<\/code> \u306e\u5024\u304c\u633f\u5165\u3055\u308c\u307e\u3059\u3002<\/span><\/li>\r\n\t<li><span style=\"font-family: arial, helvetica, sans-serif\">SQL\u30af\u30a8\u30ea\u3092\u52d5\u7684\u306b\u751f\u6210\u3059\u308b\u3053\u3068\u3067\u3001\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u3057\u3066\u540c\u3058\u51e6\u7406\u3092\u7c21\u5358\u306b\u9069\u7528\u3067\u304d\u307e\u3059\u3002<\/span><\/li>\r\n<\/ul>\r\n<b><span style=\"font-family: arial, helvetica, sans-serif\"><strong>1.2 \u5909\u6570\u306e\u6d3b\u7528<\/strong><\/span><\/b>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u5909\u6570\u3092\u4f7f\u3063\u3066\u30af\u30a8\u30ea\u306e\u6761\u4ef6\u3092\u52d5\u7684\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/span><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><span style=\"font-family: arial, helvetica, sans-serif\"><code>sql_template_with_var = \"\"\"\r\nSELECT *\r\nFROM sales_data\r\nWHERE sale_date = '{{ target_date }}'\r\n\"\"\"\r\n\r\ntemplate = Template(sql_template_with_var)\r\nrendered_sql = template.render(target_date=\"2024-01-02\")\r\nprint(rendered_sql)\r\n\r\n# \u5b9f\u884c\r\npd.read_sql(rendered_sql, conn)<\/code><\/span><\/pre>\r\n<\/div>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-6-1.png\" alt=\"\" width=\"764\" height=\"256\" class=\"alignnone size-full wp-image-5922\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-6-1.png 764w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-6-1-300x101.png 300w\" sizes=\"(max-width: 764px) 100vw, 764px\" \/><\/span><\/p>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><strong>\u89e3\u8aac:<\/strong><\/span><\/p>\r\n<ul>\r\n\t<li><span style=\"font-family: arial, helvetica, sans-serif\"><code>{{ target_date }}<\/code> \u306e\u90e8\u5206\u306b\u5909\u6570 <code>target_date<\/code> \u304c\u633f\u5165\u3055\u308c\u307e\u3059\u3002<\/span><\/li>\r\n\t<li><span style=\"font-family: arial, helvetica, sans-serif\">\u30af\u30a8\u30ea\u5b9f\u884c\u6642\u306b\u5909\u6570\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3067\u3001\u6761\u4ef6\u3092\u67d4\u8edf\u306b\u5207\u308a\u66ff\u3048\u3089\u308c\u307e\u3059\u3002<\/span><\/li>\r\n<\/ul>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><!-- notionvc: 71d100dd-534f-442a-a511-b9e4fb956359 --><\/span><\/p>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><!-- notionvc: dcd612c6-5713-4d96-bf03-55eeaaa1d9e1 --><\/span><\/p>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><!-- notionvc: 0c6e7ea0-ef97-4a3f-8b1a-32c79d990c6d --><\/span><\/p>\r\n<b><span style=\"font-family: arial, helvetica, sans-serif\"><strong>1.3 \u5236\u5fa1\u69cb\u6587 (If\u6587)<\/strong><\/span><\/b>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u6761\u4ef6\u306b\u3088\u3063\u3066\u30af\u30a8\u30ea\u306e\u5185\u5bb9\u3092\u52d5\u7684\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/span><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>sql_template_with_if = \"\"\"\r\nSELECT *\r\nFROM customer_orders\r\n{% if filter_customer %}\r\nWHERE customer_id = {{ target_customer }}\r\n{% endif %}\r\n\"\"\"\r\n\r\ntemplate = Template(sql_template_with_if)\r\nrendered_sql = template.render(filter_customer=True, target_customer=1)\r\nprint(rendered_sql)\r\n\r\n# \u5b9f\u884c\r\npd.read_sql(rendered_sql, conn)<\/code><\/pre>\r\n<\/div>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-7-1.png\" alt=\"\" width=\"864\" height=\"390\" class=\"alignnone size-full wp-image-5923\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-7-1.png 864w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-7-1-300x135.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-7-1-768x347.png 768w\" sizes=\"(max-width: 864px) 100vw, 864px\" \/><\/p>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><strong>\u89e3\u8aac:<\/strong><\/span><\/p>\r\n<ul>\r\n\t<li><span style=\"font-family: arial, helvetica, sans-serif\"><code>{% if filter_customer %}<\/code> \u306f <code>filter_customer<\/code> \u304c <code>True<\/code> \u306e\u5834\u5408\u306b\u306e\u307f\u3001<code>WHERE<\/code> \u6761\u4ef6\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3059\u3002<\/span><\/li>\r\n\t<li><span style=\"font-family: arial, helvetica, sans-serif\">\u6761\u4ef6\u5206\u5c90\u3092\u7c21\u6f54\u306b\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002<\/span><\/li>\r\n<\/ul>\r\n<b><span style=\"font-family: arial, helvetica, sans-serif\"><strong>1.4 \u30eb\u30fc\u30d7\u306e\u5229\u7528<\/strong><\/span><\/b>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u8907\u6570\u306e\u30ab\u30e9\u30e0\u3092\u5bfe\u8c61\u306b\u540c\u3058\u51e6\u7406\u3092\u884c\u3046\u5834\u5408\u3001\u30eb\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/span><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>sql_template_with_loop = \"\"\"\r\nSELECT\r\nregion,\r\n{% for quarter in quarters %}\r\nSUM({{ quarter }}) AS total_{{ quarter }}\r\n{% if not loop.last %}, {% endif %}\r\n{% endfor %}\r\nFROM product_sales\r\nGROUP BY region\r\n\"\"\"\r\n\r\ntemplate = Template(sql_template_with_loop)\r\nrendered_sql = template.render(quarters=[\"sales_q1\", \"sales_q2\", \"sales_q3\", \"sales_q4\"])\r\nprint(rendered_sql)\r\n\r\n# \u5b9f\u884c\r\npd.read_sql(rendered_sql, conn)<\/code><\/pre>\r\n<\/div>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-8-1.png\" alt=\"\" width=\"1180\" height=\"806\" class=\"alignnone size-full wp-image-5924\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-8-1.png 1180w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-8-1-300x205.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-8-1-1024x699.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-8-1-768x525.png 768w\" sizes=\"(max-width: 1180px) 100vw, 1180px\" \/><\/p>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><strong>\u89e3\u8aac:<\/strong><\/span><\/p>\r\n<ul>\r\n\t<li><span style=\"font-family: arial, helvetica, sans-serif\"><code>for<\/code> \u6587\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u8907\u6570\u306e\u30ab\u30e9\u30e0\u306b\u5bfe\u3059\u308b\u51e6\u7406\u3092\u52b9\u7387\u7684\u306b\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002<\/span><\/li>\r\n\t<li><span style=\"font-family: arial, helvetica, sans-serif\"><code>loop.last<\/code> \u3092\u4f7f\u3046\u3068\u3001\u6700\u5f8c\u306e\u8981\u7d20\u306b\u306f\u30ab\u30f3\u30de\u3092\u8ffd\u52a0\u3057\u306a\u3044\u3088\u3046\u5236\u5fa1\u3067\u304d\u307e\u3059\u3002<\/span><\/li>\r\n<\/ul>\r\n<b><span style=\"font-family: arial, helvetica, sans-serif\"><strong>1.5 \u30de\u30af\u30ed\u98a8\u306e\u518d\u5229\u7528<\/strong><\/span><\/b>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u540c\u3058\u51e6\u7406\u3092\u7e70\u308a\u8fd4\u3057\u4f7f\u3046\u5834\u5408\u3001Python\u95a2\u6570\u3092\u30de\u30af\u30ed\u306e\u3088\u3046\u306b\u6d3b\u7528\u3067\u304d\u307e\u3059\u3002<\/span><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def get_table_columns_sql(table_name):\r\nreturn f\"\"\"\r\nSELECT name AS column_name\r\nFROM pragma_table_info('{table_name}')\r\n\"\"\"\r\n\r\n# \u30de\u30af\u30ed\u547c\u3073\u51fa\u3057\r\nsql_macro = get_table_columns_sql(\"sales_data\")\r\nprint(sql_macro)\r\n\r\n# \u5b9f\u884c\r\npd.read_sql(sql_macro, conn)<\/code><\/pre>\r\n<\/div>\r\n<p><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-9-1.png\" alt=\"\" width=\"1020\" height=\"436\" class=\"alignnone size-full wp-image-5925\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-9-1.png 1020w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-9-1-300x128.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-9-1-768x328.png 768w\" sizes=\"(max-width: 1020px) 100vw, 1020px\" \/><\/p>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><strong>\u89e3\u8aac:<\/strong><\/span><\/p>\r\n<ul>\r\n\t<li><span style=\"font-family: arial, helvetica, sans-serif\">\u518d\u5229\u7528\u53ef\u80fd\u306a\u30af\u30a8\u30ea\u90e8\u5206\u3092Python\u95a2\u6570\u3068\u3057\u3066\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\u3002<\/span><\/li>\r\n\t<li><span style=\"font-family: arial, helvetica, sans-serif\">\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u7570\u306a\u308b\u30c6\u30fc\u30d6\u30eb\u540d\u3067\u30af\u30a8\u30ea\u3092\u547c\u3073\u51fa\u305b\u307e\u3059\u3002<\/span><\/li>\r\n<\/ul>\r\n<p><!-- notionvc: ecac3b02-ebd9-4161-bc91-62907747a93f --><\/p>\r\n<p>&nbsp;<\/p>\r\n<p><!-- notionvc: e73ae801-11c4-4e3d-842d-924404f92a45 --><\/p>\r\n<p><!-- notionvc: bf76f33d-1957-40de-9689-f4a68365c5b6 --><\/p>\r\n<p><!-- notionvc: b788ecd4-657e-462c-bf7f-3d35c25c69e5 --><\/p>\r\n<p><!-- notionvc: ee098f0c-f808-4a64-807f-38cd0dcdf715 --><\/p>\r\n<p><!-- notionvc: 3d0cc344-3883-4af8-a9d5-e46f5aff5efa --><\/p>\n\n<h2>2. \u5fdc\u7528: \u74b0\u5883\u306b\u5fdc\u3058\u305f\u5207\u308a\u66ff\u3048<\/h2>\n<p>&nbsp;<\/p>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\">\u672c\u756a\u74b0\u5883\u3068\u958b\u767a\u74b0\u5883\u3067\u7570\u306a\u308b\u30c6\u30fc\u30d6\u30eb\u3092\u53c2\u7167\u3059\u308b\u5834\u5408\u306e\u4f8b\u3067\u3059\u3002<\/span><\/p>\r\n<div class=\"hcb_wrap\">\r\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><span style=\"font-family: arial, helvetica, sans-serif\"><code>sql_template_env = \"\"\"\r\nSELECT *\r\nFROM {% if environment == 'prod' %}production_table{% else %}dev_table{% endif %}\r\n\"\"\"\r\n\r\ntemplate = Template(sql_template_env)\r\nrendered_sql = template.render(environment=\"dev\")\r\nprint(rendered_sql)<\/code><\/span><\/pre>\r\n<\/div>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><img decoding=\"async\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-10.png\" alt=\"\" width=\"1286\" height=\"106\" class=\"alignnone size-full wp-image-5926\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-10.png 1286w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-10-300x25.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-10-1024x84.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2024\/11\/image-10-768x63.png 768w\" sizes=\"(max-width: 1286px) 100vw, 1286px\" \/><\/span><\/p>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><strong>\u89e3\u8aac:<\/strong><\/span><\/p>\r\n<ul>\r\n\t<li><span style=\"font-family: arial, helvetica, sans-serif\"><code>{% if %}<\/code> \u6587\u3092\u4f7f\u3063\u3066\u3001\u74b0\u5883\u5909\u6570\u306b\u57fa\u3065\u3044\u3066\u30af\u30a8\u30ea\u306e\u5185\u5bb9\u3092\u52d5\u7684\u306b\u5909\u66f4\u3067\u304d\u307e\u3059\u3002<\/span><\/li>\r\n<\/ul>\r\n<p><span style=\"font-family: arial, helvetica, sans-serif\"><!-- notionvc: 236eb4a0-bcf0-4e57-947e-20fb3a2fb526 --><\/span><\/p>\r\n<p>&nbsp;<\/p>\r\n<p><!-- notionvc: f782facd-3bb1-4668-8d3c-260eae49442c --><\/p>\n\n<h2>\u7d42\u308f\u308a\u306b<\/h2>\n<p><span style=\"font-family: arial, helvetica, sans-serif\">Jinja\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001SQL\u306e\u52d5\u7684\u751f\u6210\u304c\u7c21\u5358\u306b\u884c\u3048\u308b\u3053\u3068\u3092\u4f53\u9a13\u3057\u3066\u3044\u305f\u3060\u3051\u305f\u3068\u601d\u3044\u307e\u3059\u3002\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001Python\u3092\u4f7f\u3063\u3066\u57fa\u790e\u304b\u3089\u5fdc\u7528\u307e\u3067\u5b9f\u4f8b\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002\u7279\u306b\u3001\u5909\u6570\u3084\u5236\u5fa1\u69cb\u6587\u3001\u30eb\u30fc\u30d7\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u67d4\u8edf\u3067\u52b9\u7387\u7684\u306a\u30af\u30a8\u30ea\u306e\u751f\u6210\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002 SQL\u3092\u66f8\u304f\u969b\u3001\u4f3c\u305f\u3088\u3046\u306a\u30af\u30a8\u30ea\u3092\u4f55\u5ea6\u3082\u66f8\u304f\u306e\u306f\u9762\u5012\u3067\u3059\u304c\u3001Jinja\u3092\u4f7f\u3048\u3070\u3053\u308c\u3089\u306e\u7e70\u308a\u8fd4\u3057\u3092\u81ea\u52d5\u5316\u3067\u304d\u307e\u3059\u3002\u307e\u305f\u3001\u6761\u4ef6\u306b\u5fdc\u3058\u305f\u51e6\u7406\u3084\u74b0\u5883\u3054\u3068\u306e\u5207\u308a\u66ff\u3048\u3082\u7c21\u5358\u306b\u5b9f\u73fe\u3067\u304d\u308b\u305f\u3081\u3001\u696d\u52d9\u306e\u52b9\u7387\u5316\u306b\u5927\u3044\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002 \u305c\u3072\u3053\u306e\u8a18\u4e8b\u3092\u53c2\u8003\u306b\u3001\u624b\u3092\u52d5\u304b\u3057\u306a\u304c\u3089Jinja\u3092\u5b66\u3073\u3001\u65e5\u3005\u306e\u30c7\u30fc\u30bf\u5206\u6790\u3084\u958b\u767a\u306b\u53d6\u308a\u5165\u308c\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002\u6700\u521d\u306f\u30b7\u30f3\u30d7\u30eb\u306a\u4f8b\u304b\u3089\u59cb\u3081\u3066\u3001\u5f90\u3005\u306b\u5fdc\u7528\u7684\u306a\u4f7f\u3044\u65b9\u306b\u6311\u6226\u3059\u308b\u3053\u3068\u3067\u3001Jinja\u306e\u4fbf\u5229\u3055\u3092\u5b9f\u611f\u3067\u304d\u308b\u306f\u305a\u3067\u3059\u3002<\/span><\/p>\r\n<p><!-- notionvc: 2095551d-d07c-456a-89bc-9c56142665b7 --><\/p>","protected":false},"excerpt":{"rendered":"<p>Jinja\u306fPython\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30a8\u30f3\u30b8\u30f3\u3067\u3001\u52d5\u7684\u306bSQL\u30af\u30a8\u30ea\u3092\u751f\u6210\u3067\u304d\u308b\u4fbf\u5229\u306a\u30c4\u30fc\u30eb\u3067\u3059\u3002\u8907\u96d1\u306a\u6761\u4ef6\u3084\u7e70\u308a\u8fd4\u3057\u51e6\u7406\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u8a18\u8ff0\u3067\u304d\u308b\u305f\u3081\u3001SQL\u306e\u4f5c\u6210\u3084\u30c7\u30fc\u30bf\u5206\u6790\u306e\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u307e\u3059\u3002 \u306f\u3058\u3081\u306b Jin [&hellip;]<\/p>\n","protected":false},"author":87,"featured_media":3123,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1249],"tags":[800,331,148],"class_list":["post-5919","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledge","tag-jinja","tag-python","tag-sql"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/5919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=5919"}],"version-history":[{"count":0,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/5919\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/3123"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=5919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=5919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=5919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}