{"id":974839,"date":"2024-12-27T06:08:25","date_gmt":"2024-12-26T22:08:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/974839.html"},"modified":"2024-12-27T06:08:27","modified_gmt":"2024-12-26T22:08:27","slug":"mysql%e5%a6%82%e4%bd%95%e5%ad%98%e5%82%a8%e5%9b%be%e7%89%87python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/974839.html","title":{"rendered":"mysql\u5982\u4f55\u5b58\u50a8\u56fe\u7247python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200343\/2d6ae6ff-36bd-4517-8238-8929b4ed1eaf.webp\" alt=\"mysql\u5982\u4f55\u5b58\u50a8\u56fe\u7247python\" \/><\/p>\n<p><p> <strong>\u5728MySQL\u4e2d\u5b58\u50a8\u56fe\u7247\u901a\u5e38\u6709\u51e0\u79cd\u65b9\u6cd5\uff1a\u5c06\u56fe\u7247\u5b58\u50a8\u4e3aBLOB\u6570\u636e\u7c7b\u578b\u3001\u5c06\u56fe\u7247\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u5e76\u5728\u6570\u636e\u5e93\u4e2d\u4fdd\u5b58\u8def\u5f84\u3001\u4f7f\u7528BASE64\u7f16\u7801\u5b58\u50a8\u3002\u63a8\u8350\u7684\u65b9\u5f0f\u662f\u5c06\u56fe\u7247\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u5e76\u5728MySQL\u4e2d\u5b58\u50a8\u5176\u8def\u5f84\u3002\u8fd9\u79cd\u65b9\u5f0f\u907f\u514d\u4e86\u6570\u636e\u5e93\u81a8\u80c0\u7684\u95ee\u9898\uff0c\u5e76\u4e14\u8bfb\u53d6\u901f\u5ea6\u66f4\u5feb\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u56fe\u7247\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u5e76\u5728\u6570\u636e\u5e93\u4e2d\u4fdd\u5b58\u8def\u5f84\u7684\u65b9\u6cd5\u6d89\u53ca\u5230\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u9996\u5148\uff0c\u5c06\u56fe\u7247\u4e0a\u4f20\u5230\u670d\u52a1\u5668\u5e76\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u76ee\u5f55\u4e2d\uff1b\u5176\u6b21\uff0c\u5728MySQL\u6570\u636e\u5e93\u4e2d\u521b\u5efa\u4e00\u4e2a\u8868\u683c\u7528\u4e8e\u5b58\u50a8\u56fe\u7247\u7684\u76f8\u5173\u4fe1\u606f\uff08\u5982\u6587\u4ef6\u8def\u5f84\u3001\u540d\u79f0\u7b49\uff09\uff1b\u6700\u540e\uff0c\u5728Python\u4e2d\u4f7f\u7528MySQL\u7684\u8fde\u63a5\u5e93\uff08\u5982PyMySQL\uff09\u5c06\u56fe\u7247\u8def\u5f84\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001BLOB\u6570\u636e\u7c7b\u578b\u5b58\u50a8\u56fe\u7247<\/h3>\n<\/p>\n<p><p>BLOB\uff08Binary Large Object\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u5927\u5bf9\u8c61\u6570\u636e\u7c7b\u578b\u3002\u5728MySQL\u4e2d\uff0cBLOB\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u56fe\u7247\u3001\u97f3\u9891\u3001\u89c6\u9891\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u6570\u636e\u5e93\u548c\u8868<\/strong><br \/>\u9996\u5148\uff0c\u9700\u8981\u5728MySQL\u4e2d\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u56fe\u7247\u7684\u8868\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bSQL\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sql\">CREATE TABLE Images (<\/p>\n<p>    id INT AUTO_INCREMENT PRIMARY KEY,<\/p>\n<p>    image_data LONGBLOB<\/p>\n<p>);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528LONGBLOB\u7c7b\u578b\u53ef\u4ee5\u5b58\u50a8\u6700\u5927\u957f\u5ea6\u4e3a4GB\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Python\u5b58\u50a8\u56fe\u7247<\/strong><br \/>\u4f7f\u7528Python\u8fde\u63a5MySQL\u6570\u636e\u5e93\uff0c\u5e76\u5c06\u56fe\u7247\u4ee5\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;username&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;database_name&#39;<\/p>\n<p>)<\/p>\n<p>try:<\/p>\n<p>    with connection.cursor() as cursor:<\/p>\n<p>        # \u8bfb\u53d6\u56fe\u7247\u6587\u4ef6<\/p>\n<p>        with open(&#39;path_to_image.jpg&#39;, &#39;rb&#39;) as file:<\/p>\n<p>            binary_data = file.read()<\/p>\n<p>        # \u63d2\u5165\u56fe\u7247\u6570\u636e<\/p>\n<p>        sql = &quot;INSERT INTO Images (image_data) VALUES (%s)&quot;<\/p>\n<p>        cursor.execute(sql, (binary_data,))<\/p>\n<p>    # \u63d0\u4ea4\u4e8b\u52a1<\/p>\n<p>    connection.commit()<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u548c\u663e\u793a\u56fe\u7247<\/strong><br \/>\u8981\u8bfb\u53d6\u5e76\u663e\u793a\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\uff0c\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>from PIL import Image<\/p>\n<p>import io<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;username&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;database_name&#39;<\/p>\n<p>)<\/p>\n<p>try:<\/p>\n<p>    with connection.cursor() as cursor:<\/p>\n<p>        # \u67e5\u8be2\u56fe\u7247\u6570\u636e<\/p>\n<p>        sql = &quot;SELECT image_data FROM Images WHERE id=%s&quot;<\/p>\n<p>        cursor.execute(sql, (1,))  # \u5047\u8bbe\u8bfb\u53d6id\u4e3a1\u7684\u56fe\u7247<\/p>\n<p>        result = cursor.fetchone()<\/p>\n<p>        # \u663e\u793a\u56fe\u7247<\/p>\n<p>        if result:<\/p>\n<p>            image_data = result[0]<\/p>\n<p>            image = Image.open(io.BytesIO(image_data))<\/p>\n<p>            image.show()<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u6587\u4ef6\u7cfb\u7edf\u5b58\u50a8\u56fe\u7247\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u76f8\u6bd4\u76f4\u63a5\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u56fe\u7247\u6570\u636e\uff0c\u5c06\u56fe\u7247\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u5e76\u5728\u6570\u636e\u5e93\u4e2d\u4fdd\u5b58\u8def\u5f84\u662f\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e0a\u4f20\u5e76\u4fdd\u5b58\u56fe\u7247\u5230\u6587\u4ef6\u7cfb\u7edf<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u670d\u52a1\u5668\u7aef\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\u7528\u4e8e\u5b58\u50a8\u4e0a\u4f20\u7684\u56fe\u7247\u6587\u4ef6\u3002\u901a\u5e38\uff0c\u4f7f\u7528Python\u7684Flask\u6216Django\u7b49\u6846\u67b6\u6765\u5904\u7406\u56fe\u7247\u4e0a\u4f20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from flask import Flask, request<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>UPLOAD_FOLDER = &#39;path_to_image_folder&#39;<\/p>\n<p>app.config[&#39;UPLOAD_FOLDER&#39;] = UPLOAD_FOLDER<\/p>\n<p>@app.route(&#39;\/upload&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def upload_image():<\/p>\n<p>    if &#39;file&#39; not in request.files:<\/p>\n<p>        return &#39;No file part&#39;<\/p>\n<p>    file = request.files[&#39;file&#39;]<\/p>\n<p>    if file.filename == &#39;&#39;:<\/p>\n<p>        return &#39;No selected file&#39;<\/p>\n<p>    if file:<\/p>\n<p>        file_path = os.path.join(app.config[&#39;UPLOAD_FOLDER&#39;], file.filename)<\/p>\n<p>        file.save(file_path)<\/p>\n<p>        return &#39;File uploaded successfully&#39;<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u56fe\u7247\u8def\u5f84<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u8868\u7528\u4e8e\u5b58\u50a8\u56fe\u7247\u7684\u6587\u4ef6\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sql\">CREATE TABLE ImagePaths (<\/p>\n<p>    id INT AUTO_INCREMENT PRIMARY KEY,<\/p>\n<p>    file_path VARCHAR(255)<\/p>\n<p>);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528Python\u5c06\u56fe\u7247\u8def\u5f84\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;username&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;database_name&#39;<\/p>\n<p>)<\/p>\n<p>try:<\/p>\n<p>    with connection.cursor() as cursor:<\/p>\n<p>        # \u63d2\u5165\u56fe\u7247\u8def\u5f84<\/p>\n<p>        sql = &quot;INSERT INTO ImagePaths (file_path) VALUES (%s)&quot;<\/p>\n<p>        cursor.execute(sql, (&#39;path_to_image_folder\/image.jpg&#39;,))<\/p>\n<p>    # \u63d0\u4ea4\u4e8b\u52a1<\/p>\n<p>    connection.commit()<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u56fe\u7247\u5e76\u663e\u793a<\/strong><\/p>\n<\/p>\n<p><p>\u8bfb\u53d6\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u7684\u56fe\u7247\u8def\u5f84\uff0c\u7136\u540e\u4ece\u6587\u4ef6\u7cfb\u7edf\u4e2d\u52a0\u8f7d\u56fe\u7247\u8fdb\u884c\u663e\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;username&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;database_name&#39;<\/p>\n<p>)<\/p>\n<p>try:<\/p>\n<p>    with connection.cursor() as cursor:<\/p>\n<p>        # \u67e5\u8be2\u56fe\u7247\u8def\u5f84<\/p>\n<p>        sql = &quot;SELECT file_path FROM ImagePaths WHERE id=%s&quot;<\/p>\n<p>        cursor.execute(sql, (1,))  # \u5047\u8bbe\u8bfb\u53d6id\u4e3a1\u7684\u56fe\u7247\u8def\u5f84<\/p>\n<p>        result = cursor.fetchone()<\/p>\n<p>        # \u663e\u793a\u56fe\u7247<\/p>\n<p>        if result:<\/p>\n<p>            file_path = result[0]<\/p>\n<p>            image = Image.open(file_path)<\/p>\n<p>            image.show()<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001BASE64\u7f16\u7801\u5b58\u50a8\u56fe\u7247<\/h3>\n<\/p>\n<p><p>BASE64\u7f16\u7801\u662f\u4e00\u79cd\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a\u6587\u672c\u683c\u5f0f\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728\u4e0d\u652f\u6301\u4e8c\u8fdb\u5236\u7684\u7cfb\u7edf\u4e2d\u4f20\u8f93\u56fe\u7247\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5c06\u56fe\u7247\u7f16\u7801\u4e3aBASE64<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5c06\u56fe\u7247\u6587\u4ef6\u7f16\u7801\u4e3aBASE64\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import base64<\/p>\n<p>with open(&#39;path_to_image.jpg&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    encoded_string = base64.b64encode(file.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8BASE64\u7f16\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u8868\u7528\u4e8e\u5b58\u50a8BASE64\u7f16\u7801\u7684\u56fe\u7247\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sql\">CREATE TABLE Base64Images (<\/p>\n<p>    id INT AUTO_INCREMENT PRIMARY KEY,<\/p>\n<p>    image_base64 LONGTEXT<\/p>\n<p>);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528Python\u5c06BASE64\u7f16\u7801\u7684\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;username&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;database_name&#39;<\/p>\n<p>)<\/p>\n<p>try:<\/p>\n<p>    with connection.cursor() as cursor:<\/p>\n<p>        # \u63d2\u5165BASE64\u7f16\u7801\u7684\u6570\u636e<\/p>\n<p>        sql = &quot;INSERT INTO Base64Images (image_base64) VALUES (%s)&quot;<\/p>\n<p>        cursor.execute(sql, (encoded_string.decode(&#39;utf-8&#39;),))<\/p>\n<p>    # \u63d0\u4ea4\u4e8b\u52a1<\/p>\n<p>    connection.commit()<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u89e3\u7801\u5e76\u663e\u793a\u56fe\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6BASE64\u7f16\u7801\u7684\u6570\u636e\uff0c\u5e76\u89e3\u7801\u4e3a\u539f\u59cb\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u4ee5\u663e\u793a\u56fe\u7247\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>from PIL import Image<\/p>\n<p>import io<\/p>\n<p>import base64<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;username&#39;,<\/p>\n<p>    password=&#39;password&#39;,<\/p>\n<p>    database=&#39;database_name&#39;<\/p>\n<p>)<\/p>\n<p>try:<\/p>\n<p>    with connection.cursor() as cursor:<\/p>\n<p>        # \u67e5\u8be2BASE64\u7f16\u7801\u7684\u6570\u636e<\/p>\n<p>        sql = &quot;SELECT image_base64 FROM Base64Images WHERE id=%s&quot;<\/p>\n<p>        cursor.execute(sql, (1,))  # \u5047\u8bbe\u8bfb\u53d6id\u4e3a1\u7684\u56fe\u7247<\/p>\n<p>        result = cursor.fetchone()<\/p>\n<p>        # \u89e3\u7801\u5e76\u663e\u793a\u56fe\u7247<\/p>\n<p>        if result:<\/p>\n<p>            image_base64 = result[0]<\/p>\n<p>            image_data = base64.b64decode(image_base64)<\/p>\n<p>            image = Image.open(io.BytesIO(image_data))<\/p>\n<p>            image.show()<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u5b58\u50a8\u56fe\u7247\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6027\u80fd\u548c\u6548\u7387<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u56fe\u7247\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u5e76\u5728\u6570\u636e\u5e93\u4e2d\u4fdd\u5b58\u8def\u5f84\uff0c\u901a\u5e38\u6bd4\u5c06\u56fe\u7247\u5b58\u50a8\u4e3aBLOB\u6216BASE64\u66f4\u9ad8\u6548\u3002\u8fd9\u662f\u56e0\u4e3a\u6587\u4ef6\u7cfb\u7edf\u66f4\u52a0\u64c5\u957f\u5904\u7406\u5927\u578b\u6587\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7CDN\u52a0\u901f\u56fe\u7247\u7684\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u5168\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5b58\u50a8\u56fe\u7247\u8def\u5f84\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8def\u5f84\u7684\u5b89\u5168\u6027\uff0c\u907f\u514d\u8def\u5f84\u6cc4\u9732\u5bfc\u81f4\u7684\u56fe\u7247\u88ab\u672a\u6388\u6743\u8bbf\u95ee\u3002\u5728Web\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bbf\u95ee\u63a7\u5236\u548c\u6388\u6743\u673a\u5236\u6765\u4fdd\u62a4\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5907\u4efd\u548c\u6062\u590d<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u6027\uff0c\u9700\u8981\u5b9a\u671f\u5907\u4efd\u6570\u636e\u5e93\u548c\u6587\u4ef6\u7cfb\u7edf\u4e2d\u7684\u56fe\u7247\u6587\u4ef6\u3002\u5907\u4efd\u65f6\u53ef\u4ee5\u4f7f\u7528\u589e\u91cf\u5907\u4efd\u7684\u65b9\u6cd5\uff0c\u4ee5\u51cf\u5c11\u5907\u4efd\u65f6\u95f4\u548c\u5b58\u50a8\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u7247\u7684\u7f29\u7565\u56fe\u548c\u683c\u5f0f\u8f6c\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u56fe\u7247\u52a0\u8f7d\u901f\u5ea6\uff0c\u53ef\u4ee5\u5728\u4e0a\u4f20\u56fe\u7247\u65f6\u751f\u6210\u7f29\u7565\u56fe\uff0c\u5e76\u5728Web\u5e94\u7528\u4e2d\u4f18\u5148\u5c55\u793a\u7f29\u7565\u56fe\u3002\u8fd8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8f6c\u6362\u56fe\u7247\u683c\u5f0f\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u8bbe\u5907\u7684\u663e\u793a\u8981\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u4e4b\uff0c\u5b58\u50a8\u56fe\u7247\u7684\u9009\u62e9\u5e94\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u3001\u6027\u80fd\u8981\u6c42\u548c\u6570\u636e\u91cf\u6765\u51b3\u5b9a\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5c06\u56fe\u7247\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u5e76\u5728\u6570\u636e\u5e93\u4e2d\u4fdd\u5b58\u8def\u5f84\u662f\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u548c\u4fbf\u6377\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728MySQL\u4e2d\u5b58\u50a8\u56fe\u7247\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728MySQL\u4e2d\u5b58\u50a8\u56fe\u7247\u901a\u5e38\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u5c06\u56fe\u7247\u4f5c\u4e3aBLOB\uff08\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\uff09\u7c7b\u578b\u5b58\u50a8\uff0c\u6216\u8005\u5c06\u56fe\u7247\u6587\u4ef6\u7684\u8def\u5f84\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u3002\u4f7f\u7528BLOB\u5b58\u50a8\u56fe\u7247\u7684\u4f18\u70b9\u662f\u6570\u636e\u6574\u5408\u6027\u5f3a\uff0c\u4f46\u7f3a\u70b9\u662f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u5e93\u4f53\u79ef\u81a8\u80c0\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u56fe\u7247\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u5e76\u5728\u6570\u636e\u5e93\u4e2d\u4fdd\u5b58\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c0f\u6570\u636e\u5e93\u7684\u5927\u5c0f\uff0c\u4f46\u9700\u8981\u786e\u4fdd\u6587\u4ef6\u7cfb\u7edf\u7684\u5b89\u5168\u6027\u548c\u6587\u4ef6\u7684\u53ef\u8bbf\u95ee\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u56fe\u7247\u5b58\u50a8\u5230MySQL\u6570\u636e\u5e93\u4e2d\uff1f<\/strong><br \/>\u4f7f\u7528Python\u5c06\u56fe\u7247\u5b58\u50a8\u5230MySQL\u6570\u636e\u5e93\u4e2d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5<code>mysql-connector-python<\/code>\u6216<code>PyMySQL<\/code>\u7b49\u5e93\u3002\u63a5\u4e0b\u6765\uff0c\u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u5305\u542bBLOB\u5b57\u6bb5\u7684\u8868\u3002\u901a\u8fc7\u8bfb\u53d6\u56fe\u7247\u6587\u4ef6\u7684\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u5c06\u5176\u63d2\u5165\u5230\u6570\u636e\u5e93\u4e2d\u3002\u793a\u4f8b\u4ee3\u7801\u53ef\u4ee5\u53c2\u8003\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import mysql.connector\n\ndef insert_image(image_path):\n    conn = mysql.connector.connect(user=&#39;your_user&#39;, password=&#39;your_password&#39;,\n                                   host=&#39;your_host&#39;, database=&#39;your_database&#39;)\n    cursor = conn.cursor()\n    with open(image_path, &#39;rb&#39;) as file:\n        binary_data = file.read()\n        cursor.execute(&quot;INSERT INTO images (image_data) VALUES (%s)&quot;, (binary_data,))\n    conn.commit()\n    cursor.close()\n    conn.close()\n<\/code><\/pre>\n<p><strong>\u5728MySQL\u4e2d\u5b58\u50a8\u56fe\u7247\u4f1a\u5f71\u54cd\u6570\u636e\u5e93\u6027\u80fd\u5417\uff1f<\/strong><br \/>\u5728MySQL\u4e2d\u5b58\u50a8\u5927\u91cf\u56fe\u7247\u4f1a\u5bf9\u6570\u636e\u5e93\u6027\u80fd\u4ea7\u751f\u5f71\u54cd\uff0c\u5c24\u5176\u662f\u5728\u68c0\u7d22\u548c\u5907\u4efd\u6570\u636e\u65f6\u3002\u5b58\u50a8BLOB\u6570\u636e\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u5e93\u7684\u8bfb\u53d6\u901f\u5ea6\u53d8\u6162\uff0c\u5c24\u5176\u662f\u5f53\u6570\u636e\u91cf\u5e9e\u5927\u65f6\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u8bbe\u8ba1\u6570\u636e\u5e93\u67b6\u6784\u65f6\uff0c\u8bc4\u4f30\u5b58\u50a8\u56fe\u7247\u7684\u9700\u6c42\uff0c\u5e76\u8003\u8651\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u5b58\u50a8\u56fe\u7247\uff0c\u5e76\u5c06\u8def\u5f84\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u7ba1\u7406\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728MySQL\u4e2d\u5b58\u50a8\u56fe\u7247\u901a\u5e38\u6709\u51e0\u79cd\u65b9\u6cd5\uff1a\u5c06\u56fe\u7247\u5b58\u50a8\u4e3aBLOB\u6570\u636e\u7c7b\u578b\u3001\u5c06\u56fe\u7247\u5b58\u50a8\u5728\u6587\u4ef6\u7cfb\u7edf\u4e2d\u5e76\u5728\u6570\u636e\u5e93\u4e2d\u4fdd\u5b58\u8def\u5f84 [&hellip;]","protected":false},"author":3,"featured_media":974845,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974839"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=974839"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974839\/revisions"}],"predecessor-version":[{"id":974849,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974839\/revisions\/974849"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/974845"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=974839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=974839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=974839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}