{"id":204792,"date":"2025-05-29T10:24:58","date_gmt":"2025-05-29T02:24:58","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204792\/"},"modified":"2025-05-29T10:24:58","modified_gmt":"2025-05-29T02:24:58","slug":"%e4%bd%bf%e7%94%a8-flask-%e5%92%8c-mysql-%e7%9a%84%e4%bb%bb%e5%8a%a1%e7%ae%a1%e7%90%86%e5%99%a8%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204792\/","title":{"rendered":"\u4f7f\u7528 Flask \u548c MySQL \u7684\u4efb\u52a1\u7ba1\u7406\u5668\u5e94\u7528\u7a0b\u5e8f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528 Flask \u548c MySQL \u7684\u4efb\u52a1\u7ba1\u7406\u5668\u5e94\u7528\u7a0b\u5e8f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 Flask \u548c MySQL \u7684\u4efb\u52a1\u7ba1\u7406\u5668\u5e94\u7528\u7a0b\u5e8f\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p>\u8fd9\u4e2a\u9879\u76ee\u662f\u4e00\u4e2a\u4f7f\u7528 flask \u548c mysql \u6784\u5efa\u7684<strong>\u4efb\u52a1\u7ba1\u7406\u5668\u5e94\u7528\u7a0b\u5e8f<\/strong>\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 restful api \u6765\u7ba1\u7406\u4efb\u52a1\uff0c\u6f14\u793a\u4e86\u57fa\u672c\u7684 crud\uff08\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u5220\u9664\uff09\u64cd\u4f5c\u3002 <\/p>\n<p>\u6b64\u5e94\u7528\u7a0b\u5e8f\u975e\u5e38\u9002\u5408\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 docker \u5c06 flask \u5e94\u7528\u7a0b\u5e8f\u5bb9\u5668\u5316\u5e76\u4e0e mysql \u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<ul>\n<li>\u6dfb\u52a0\u65b0\u4efb\u52a1<\/li>\n<li>\u67e5\u770b\u6240\u6709\u4efb\u52a1<\/li>\n<li>\u901a\u8fc7id\u5220\u9664\u4efb\u52a1<\/li>\n<\/ul>\n<pre>from flask import flask, request, jsonify\nimport mysql.connector\nfrom mysql.connector import error\n\napp = flask(__name__)\n\n# database connection function\ndef get_db_connection():\n    try:\n        connection = mysql.connector.connect(\n            host=\"db\",\n            user=\"root\",\n            password=\"example\",\n            database=\"task_db\"\n        )\n        return connection\n    except error as e:\n        return str(e)\n\n# route for the home page\n@app.route('\/')\ndef home():\n    return \"welcome to the task management api! use \/tasks to interact with tasks.\"\n\n# route to create a new task\n@app.route('\/tasks', methods=['post'])\ndef add_task():\n    task_description = request.json.get('description')\n    if not task_description:\n        return jsonify({\"error\": \"task description is required\"}), 400\n\n    connection = get_db_connection()\n    if isinstance(connection, str):  # if connection fails\n        return jsonify({\"error\": connection}), 500\n\n    cursor = connection.cursor()\n    cursor.execute(\"insert into tasks (description) values (%s)\", (task_description,))\n    connection.commit()\n    task_id = cursor.lastrowid\n    cursor.close()\n    connection.close()\n\n    return jsonify({\"message\": \"task added successfully\", \"task_id\": task_id}), 201\n\n# route to get all tasks\n@app.route('\/tasks', methods=['get'])\ndef get_tasks():\n    connection = get_db_connection()\n    if isinstance(connection, str):  # if connection fails\n        return jsonify({\"error\": connection}), 500\n\n    cursor = connection.cursor()\n    cursor.execute(\"select id, description from tasks\")\n    tasks = cursor.fetchall()\n    cursor.close()\n    connection.close()\n\n    task_list = [{\"id\": task[0], \"description\": task[1]} for task in tasks]\n    return jsonify(task_list), 200\n\n# route to delete a task by id\n@app.route('\/tasks\/&lt;int:task_id&gt;', methods=['delete'])\ndef delete_task(task_id):\n    connection = get_db_connection()\n    if isinstance(connection, str):  # if connection fails\n        return jsonify({\"error\": connection}), 500\n\n    cursor = connection.cursor()\n    cursor.execute(\"delete from tasks where id = %s\", (task_id,))\n    connection.commit()\n    cursor.close()\n    connection.close()\n\n    return jsonify({\"message\": \"task deleted successfully\"}), 200\n\nif __name__ == \"__main__\":\n    app.run(host='0.0.0.0')\n\n\n<\/pre>\n<p>\u521b\u5efa\u540d\u4e3a init-db.sql \u7684 mysql \u811a\u672c\u6765\u8bbe\u7f6e\u6570\u636e\u5e93\u548c\u4efb\u52a1\u8868\uff1a<\/p>\n<p>\u8981\u521b\u5efa init-db.sql \u811a\u672c\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684<strong>\u6587\u4ef6<\/strong>\uff1a<\/p>\n<p>\u5bfc\u822a\u5230\u9879\u76ee\u6587\u4ef6\u5939\u5e76\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <strong><em>init-db.sql<\/em><\/strong><br \/> \u7684\u65b0\u6587\u4ef6 \u6dfb\u52a0 <strong>sql<\/strong> \u547d\u4ee4\u6765\u8bbe\u7f6e\u6570\u636e\u5e93\u548c\u4efb\u52a1\u8868\uff1a<\/p>\n<p>\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u6253\u5f00 <strong><em>init-db.sql<\/em><\/strong> \u5e76\u6dfb\u52a0\u4ee5\u4e0b sql \u547d\u4ee4\uff1a<\/p>\n<pre>\ncreate database if not exists task_db;\nuse task_db;\n\ncreate table if not exists tasks (\n    id int auto_increment primary key,\n    description varchar(255) not null\n);\n\n<\/pre>\n<p>\u6211\u5c06\u6587\u4ef6\u4fdd\u5b58\u4e3a <strong><em>init-db.sql<\/em><\/strong> \u4f4d\u4e8e\u6211\u7684 <strong><em>docker-compose.yml<\/em><\/strong> \u6240\u5728\u7684\u9879\u76ee\u6587\u4ef6\u5939\u4e2d.<\/p>\n<p><strong>\u5728 docker-compose.yml \u4e2d\uff1a<\/strong><\/p>\n<p>\u5728\u6211\u7684 <strong><em>docker-compose.yml<\/em><\/strong> \u6587\u4ef6\u4e2d\uff0c\u6211\u6709\u6307\u5411\u6b64\u811a\u672c\u7684\u5377\u914d\u7f6e\u3002<\/p>\n<p>\u4e0b\u9762\u662f<strong><em>docker-compose.yml<\/em><\/strong>\u6587\u4ef6<\/p>\n<p><strong><em>docker-compose.yml:<\/em><\/strong><\/p>\n<pre>version: '3'\nservices:\n  db:\n    image: mysql:5.7\n    environment:\n      mysql_root_password: example\n      mysql_database: task_db\n    ports:\n      - \"3306:3306\"\n    volumes:\n      - db_data:\/var\/lib\/mysql\n      - .\/init-db.sql:\/docker-entrypoint-initdb.d\/init-db.sql\n\n  web:\n    build: .\n    ports:\n      - \"5000:5000\"\n    depends_on:\n      - db\n    environment:\n      flask_env: development\n    volumes:\n      - .:\/app\n\nvolumes:\n  db_data:\n\n<\/pre>\n<p>\u6b64\u914d\u7f6e\u786e\u4fdd\u5f53 mysql \u5bb9\u5668\u542f\u52a8\u65f6\uff0c\u5b83\u5c06\u6267\u884c <strong><em>init-db.sql<\/em><\/strong> \u811a\u672c\u6765\u8bbe\u7f6e <strong><em>task_db<\/em><\/strong> \u6570\u636e\u5e93\u5e76\u521b\u5efa <strong>tasks<\/strong> \u8868\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><strong><em>docker-entrypoint-initdb.d\/<\/em><\/strong>\u76ee\u5f55\u88ab<strong>mysql<\/strong>\u5bb9\u5668\u7528\u6765\u6267\u884c<strong><em>.sql<\/em><\/strong> \u5bb9\u5668\u521d\u59cb\u542f\u52a8\u671f\u95f4\u7684\u811a\u672c\u3002<\/p>\n<p><strong>1\u3002 version: &#8216;3&#8217;:<\/strong> \u6307\u5b9a\u6b63\u5728\u4f7f\u7528\u7684 docker compose \u7248\u672c\u3002<\/p>\n<p><strong>2\u3002\u670d\u52a1\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u6570\u636e\u5e93\uff1a<\/p>\n<ul>\n<li> <strong>image: mysql:5.7:<\/strong> \u4f7f\u7528 mysql 5.7 \u955c\u50cf\u3002<\/li>\n<li> <strong>\u73af\u5883\uff1a<\/strong> \u8bbe\u7f6e mysql \u5bb9\u5668\u7684\u73af\u5883\u53d8\u91cf\uff1a\n<ul>\n<li> <strong>mysql_root_password:<\/strong> mysql \u7684 root \u5bc6\u7801\u3002<\/li>\n<li> <strong>mysql_database:<\/strong>\u542f\u52a8\u65f6\u521b\u5efa\u7684\u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n<\/li>\n<li> <strong>ports:<\/strong> \u5c06 mysql \u5bb9\u5668\u7684\u7aef\u53e3 3306 \u6620\u5c04\u5230\u4e3b\u673a\u7684\u7aef\u53e3 3306\u3002<\/li>\n<li> <strong>\u5377\uff1a<\/strong>\n<ul>\n<li> <strong><em>db_data:\/var\/lib\/mysql:<\/em><\/strong> \u5c06\u6570\u636e\u5e93\u6570\u636e\u4fdd\u5b58\u5728\u540d\u4e3a <strong><em>db_data.<\/em><\/strong> \u7684 docker \u5377\u4e2d <\/li>\n<li> <strong><em>.\/init-db.sql:\/docker-entrypoint-initdb.d\/init-db.sql:<\/em><\/strong> \u6302\u8f7d <strong><em>init-db.sql<\/em><\/strong> \u811a\u672c\u653e\u5165 <strong>mysql<\/strong> \u5bb9\u5668\u7684\u521d\u59cb\u5316\u76ee\u5f55\u4e2d\uff0c\u4ee5\u4fbf\u5728\u5bb9\u5668\u542f\u52a8\u65f6\u8fd0\u884c\u5f00\u59cb\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7f51\u9875\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>build:<\/strong> .: \u4f7f\u7528\u5f53\u524d\u76ee\u5f55\u4e2d\u7684 <strong><em>dockerfile<\/em><\/strong> \u4e3a\u60a8\u7684 flask \u5e94\u7528\u6784\u5efa docker \u955c\u50cf\u3002<\/li>\n<li> <strong>\u7aef\u53e3\uff1a<\/strong> \u5c06 flask \u5e94\u7528\u7a0b\u5e8f\u7684\u7aef\u53e3 5000 \u6620\u5c04\u5230\u4e3b\u673a\u7684\u7aef\u53e3 5000\u3002<\/li>\n<li> <strong>depends_on:<\/strong> \u786e\u4fdd <strong><em>db<\/em><\/strong> \u670d\u52a1\u5728 web \u670d\u52a1\u4e4b\u524d\u542f\u52a8\u3002<\/li>\n<li> <strong>\u73af\u5883\uff1a<\/strong> \u8bbe\u7f6e flask \u7684\u73af\u5883\u53d8\u91cf\u3002<\/li>\n<li> <strong>volumes:<\/strong> \u5c06\u5f53\u524d\u9879\u76ee\u76ee\u5f55\u6302\u8f7d\u5230\u5bb9\u5668\u5185\u7684 <strong><em>\/app<\/em><\/strong> \u76ee\u5f55\u4e2d\u3002 ### \u5377\u90e8\u5206\uff1a <strong>db_data:<\/strong> \u5b9a\u4e49\u4e00\u4e2a\u547d\u540d\u5377 db_data \u4ee5\u5728\u5bb9\u5668\u91cd\u65b0\u542f\u52a8\u4e4b\u95f4\u4fdd\u7559 mysql \u6570\u636e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u5b9a\u4e49 flask \u5e94\u7528\u7a0b\u5e8f\u7684\u6784\u5efa\u6307\u4ee4\uff1a<\/p>\n<pre>from python:3.9-slim\n\nworkdir \/app\n\n# install dependencies\n\ncopy requirements.txt .\nrun pip install -r requirements.txt\n\n# install wait-for-it tool#\n\nrun apt-get update &amp;&amp; apt-get install -y wait-for-it\n\n#copy the application code&gt;\n\ncopy . .\n\n# use wait-for-it to wait for db and start the flask app\n\ncmd [\"wait-for-it\", \"db:3306\", \"--\", \"python\", \"app.py\"]\n\n<\/pre>\n<p>\u8fd9\u4e2a dockerfile \u4e3a flask \u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684 python \u73af\u5883\uff1a<\/p>\n<p><strong>1\u3002\u57fa\u7840\u955c\u50cf\uff1a<\/strong> \u4f7f\u7528 python:3.9-slim \u6765\u5b9e\u73b0\u6700\u77ed\u7684 python \u8fd0\u884c\u65f6\u95f4\u3002<br \/> \u5de5\u4f5c\u76ee\u5f55\uff1a\u5c06 \/app \u8bbe\u7f6e\u4e3a\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<p><strong>2\u3002\u4f9d\u8d56\u9879\uff1a<\/strong>\u590d\u5236requirements.txt\u5e76\u901a\u8fc7pip\u5b89\u88c5\u4f9d\u8d56\u9879\u3002<\/p>\n<p><strong>3\u3002\u5de5\u5177\u5b89\u88c5\uff1a<\/strong> \u5b89\u88c5 wait-for-it \u4ee5\u68c0\u67e5\u670d\u52a1\u51c6\u5907\u60c5\u51b5\u3002<\/p>\n<p><strong>4\u3002\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\uff1a<\/strong> \u5c06\u6240\u6709\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u590d\u5236\u5230\u5bb9\u5668\u4e2d\u3002<\/p>\n<p><strong>5\u3002\u542f\u52a8\u547d\u4ee4\uff1a<\/strong> \u8fd0\u884c wait-for-it \u4ee5\u786e\u4fdd mysql db (db:3306) \u5728\u542f\u52a8 app.py \u4e4b\u524d\u51c6\u5907\u5c31\u7eea\u3002<\/p>\n<p>\u6b64 <strong><em>requirements.txt<\/em><\/strong> \u6307\u5b9a python \u9879\u76ee\u9700\u8981 <strong>flask \u6846\u67b6<\/strong> \u6765\u6784\u5efa web \u5e94\u7528\u7a0b\u5e8f\u548c <strong><em>mysql-connector-python <\/em><\/strong> \u7528\u4e8e\u4e0e <strong>mysql \u6570\u636e\u5e93<\/strong> \u8fde\u63a5\u548c\u4ea4\u4e92\u3002\u5f53\u5728\u955c\u50cf\u6784\u5efa\u8fc7\u7a0b\u4e2d\u8fd0\u884c <strong><\/strong>pip install -rrequirements.txt<strong><em> \u65f6\uff0c\u8fd9\u4e9b\u5305\u5c06\u5b89\u88c5\u5728 <\/em>docker<\/strong> \u5bb9\u5668\u4e2d\u3002\u8fd9\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u62e5\u6709\u8fd0\u884c <strong>flask \u670d\u52a1\u5668<\/strong> \u5e76\u4e0e <strong>mysql \u6570\u636e\u5e93<\/strong> \u901a\u4fe1\u6240\u9700\u7684\u5de5\u5177\u3002<\/p>\n<pre>flask\nmysql-connector-python\n\n<\/pre>\n<p>\u521b\u5efa\u6240\u6709\u6587\u4ef6\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u6784\u5efa\u5e76\u8fd0\u884c\u670d\u52a1\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u6784\u5efa\u548c\u8fd0\u884c\u670d\u52a1\u3002<\/p>\n<pre>docker-compose build\ndocker-compose up\n\n<\/pre>\n<p>\u8981\u4ee5\u5206\u79bb\u6a21\u5f0f\u8fd0\u884c\u670d\u52a1\uff0c\u6211\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u800c\u4e0d\u662f <strong><em>docker-compose up<\/em><\/strong><\/p>\n<pre>docker-compose up -d\n<\/pre>\n<p>\u5f53\u6211\u60f3\u505c\u6b62\u670d\u52a1\u65f6\uff0c\u6211\u4f7f\u7528\u547d\u4ee4<\/p>\n<pre>docker-compose down\n<\/pre>\n<p>\u73b0\u5728\uff0c\u4e00\u65e6\u670d\u52a1\u5904\u4e8e\u8fd0\u884c\u72b6\u6001\uff0c\u8bf7\u8fd0\u884c\u547d\u4ee4<\/p>\n<pre>docker ps \n<\/pre>\n<p>\u786e\u4fdd\u5bb9\u5668\u6b63\u5728\u8fd0\u884c<\/p>\n<p>\u73b0\u5728\u662f\u65f6\u5019\u68c0\u67e5\u670d\u52a1 api \u4ee5\u786e\u4fdd\u5b83\u4eec\u6309\u9884\u671f\u5de5\u4f5c\u4e86\u3002<\/p>\n<p>\u901a\u8fc7 http:\/\/localhost:5000\/ \u8bbf\u95ee\u5e94\u7528\u7a0b\u5e8f\u3002<br \/> \u8fd0\u884c\u4e0a\u8ff0\u547d\u4ee4\u540e\uff0c\u6211\u80fd\u591f\u5728\u6d4f\u89c8\u5668\u4e0a\u8bbf\u95ee\u8be5\u5e94\u7528\u7a0b\u5e8f\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/17317417126738481059923.jpg\" class=\"aligncenter\"><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 postman \u6216 curl \u6765\u6d4b\u8bd5 \/tasks \u7aef\u70b9\u7684 post\u3001get \u548c delete \u64cd\u4f5c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4f1a\u4f7f\u7528curl\u3002<\/p>\n<ul>\n<li><strong><em>\u83b7\u53d6\u4efb\u52a1\uff1a<\/em><\/strong><\/li>\n<\/ul>\n<p>get \u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u4efb\u52a1\u3002<\/p>\n<pre>curl http:\/\/localhost:5000\/tasks\n\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/1731741712673848105fff4.jpg\" class=\"aligncenter\"><\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u6bcf\u5f53\u60a8\u5728\u6d4f\u89c8\u5668\u4e0a\u8fd0\u884c http:\/\/localhost:5000\/tasks \u65f6\uff0c\u5b83\u90fd\u4f1a\u663e\u793a\u60a8\u5df2\u6dfb\u52a0\u7684\u6240\u6709\u4efb\u52a1\uff0c\u5982\u6dfb\u52a0\u4efb\u52a1\u4e2d\u6240\u8ff0\u3002<\/p>\n<ul>\n<li><strong><em>\u6dfb\u52a0\u4efb\u52a1\uff1a<\/em><\/strong><\/li>\n<\/ul>\n<p>post \u65b9\u6cd5\u5728\u6570\u636e\u5e93\u4e2d\u521b\u5efa\u4efb\u52a1\u3002<\/p>\n<pre>curl -x post http:\/\/localhost:5000\/tasks -h \"content-type: application\/json\" -d '{\"description\": \"sample task\"}'\n<\/pre>\n<p>\u8fd9\u5c06\u5411\u60a8\u7684 flask \u5e94\u7528\u53d1\u9001\u5e26\u6709\u4efb\u52a1\u63cf\u8ff0\u7684 post \u8bf7\u6c42\u3002\u5982\u679c\u4efb\u52a1\u6dfb\u52a0\u6210\u529f\uff0c\u60a8\u5e94\u8be5\u6536\u5230\u5982\u4e0b\u54cd\u5e94\uff1a<\/p>\n<pre>{\n    \"message\": \"task added successfully\",\n    \"task_id\": 1\n}\n<\/pre>\n<p>\u68c0\u67e5\u6d4f\u89c8\u5668\u7684\u7f51\u7edc\u9009\u9879\u5361\u6216\u65e5\u5fd7\u4ee5\u9a8c\u8bc1 post \u8bf7\u6c42\u662f\u5426\u6b63\u786e\u53d1\u51fa\u3002<\/p>\n<p>\u6211\u8fd0\u884c\u4e86\u8be5\u547d\u4ee4\u51e0\u6b21\uff0c\u5e76\u81ea\u5b9a\u4e49\u4e86\u201c\u7b80\u5355\u4efb\u52a1\u201d\u90e8\u5206\u4ee5\u751f\u6210\u4e0d\u540c\u7684\u8f93\u51fa\uff0c\u8fd9\u662f\u6211\u8fd0\u884c\u7684\u547d\u4ee4\uff0c\u8f93\u51fa\u53ef\u4ee5\u5728\u4e0b\u56fe\u4e2d\u770b\u5230\u3002<\/p>\n<pre> curl -x post http:\/\/localhost:5000\/tasks -h \"content-type: application\/json\" -d '{\"description\": \"my name is matthew tarfa am the cloud chief\"}'\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/17317417126738481062baa.jpg\" class=\"aligncenter\"><\/p>\n<pre> curl -x post http:\/\/localhost:5000\/tasks -h \"content-type: application\/json\" -d '{\"description\": \"welcome to my world!\"}'\n\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/17317417126738481067dfa.jpg\" class=\"aligncenter\"><\/p>\n<pre> curl -x post http:\/\/localhost:5000\/tasks -h \"content-type: application\/json\" -d '{\"description\": \"i love devops!\"}'\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/1731741712673848106d14c.jpg\" class=\"aligncenter\"><\/p>\n<ul>\n<li><strong><em>\u5220\u9664\u4efb\u52a1\uff1a<\/em><\/strong><\/li>\n<\/ul>\n<p>delete \u65b9\u6cd5\u901a\u8fc7 id \u5220\u9664\u4efb\u52a1\u3002<\/p>\n<pre>curl -x delete http:\/\/localhost:5000\/tasks\/1\n\n<\/pre>\n<p>\u6211\u8fd0\u884c\u4e86\u4ee5\u4e0b\u547d\u4ee4\u6765\u5220\u9664 id \u4e3a 4 \u7684\u4efb\u52a1\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4efb\u52a1 4 \u5df2\u88ab\u5220\u9664\u3002<\/p>\n<pre>curl -X DELETE http:\/\/localhost:5000\/tasks\/4\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/17317417126738481070751.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4f7f\u7528 flask \u548c mysql \u521b\u5efa\u4efb\u52a1\u7ba1\u7406\u5668\u5e94\u7528\u7a0b\u5e8f\u662f\u4e86\u89e3 web \u670d\u52a1\u5f00\u53d1\u3001\u6570\u636e\u5e93\u96c6\u6210\u548c docker \u5bb9\u5668\u5316\u57fa\u7840\u77e5\u8bc6\u7684\u7edd\u4f73\u65b9\u6cd5\u3002 <\/p>\n<p>\u8be5\u9879\u76ee\u5c01\u88c5\u4e86 web \u670d\u52a1\u5668\u548c\u6570\u636e\u5e93\u5982\u4f55\u534f\u540c\u5de5\u4f5c\u4ee5\u63d0\u4f9b\u65e0\u7f1d\u529f\u80fd\u3002 <\/p>\n<p>\u62e5\u62b1\u8fd9\u79cd\u5b66\u4e60\u4f53\u9a8c\uff0c\u5e76\u5c06\u5176\u7528\u4f5c\u66f4\u6df1\u5c42\u6b21\u7684\u7f51\u7edc\u548c\u57fa\u4e8e\u4e91\u7684\u5f00\u53d1\u9879\u76ee\u7684\u57ab\u811a\u77f3\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4f7f\u7528 Flask \u548c MySQL \u7684\u4efb\u52a1\u7ba1\u7406\u5668\u5e94\u7528\u7a0b\u5e8f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Flask \u548c MySQL&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204792","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204792"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204792\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}