{"id":152245,"date":"2024-11-01T21:19:23","date_gmt":"2024-11-01T13:19:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/152245\/"},"modified":"2024-11-01T21:19:24","modified_gmt":"2024-11-01T13:19:24","slug":"%e4%b8%8d%e5%90%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e5%b0%8dblob%e5%ad%97%e6%ae%b5%e7%9a%84%e8%99%95%e7%90%86%e4%bb%a3%e7%a2%bc%e6%bc%94%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/152245\/","title":{"rendered":"\u4e0d\u540c\u6578\u64da\u5eab\u5c0dblob\u5b57\u6bb5\u7684\u8655\u7406\u4ee3\u78bc\u6f14\u793a"},"content":{"rendered":"<h1 id=\"%e4%b8%8d%e5%90%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e5%b0%8dblob%e5%ad%97%e6%ae%b5%e7%9a%84%e8%99%95%e7%90%86%e4%bb%a3%e7%a2%bc%e6%bc%94%e7%a4%ba-YQQmcXqtGK\">\u4e0d\u540c\u6578\u64da\u5eab\u5c0dBLOB\u5b57\u6bb5\u7684\u8655\u7406\u4ee3\u78bc\u6f14\u793a<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0cBLOB\uff08Binary Large Object\uff09\u5b57\u6bb5\u88ab\u5ee3\u6cdb\u7528\u65bc\u5b58\u5132\u5927\u91cf\u7684\u4e8c\u9032\u5236\u6578\u64da\uff0c\u4f8b\u5982\u5716\u7247\u3001\u97f3\u983b\u548c\u8996\u983b\u6587\u4ef6\u3002\u4e0d\u540c\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u5c0dBLOB\u5b57\u6bb5\u7684\u8655\u7406\u65b9\u5f0f\u5404\u6709\u4e0d\u540c\uff0c\u672c\u6587\u5c07\u63a2\u8a0e\u5e7e\u7a2e\u4e3b\u6d41\u6578\u64da\u5eab\uff08\u5982MySQL\u3001PostgreSQL\u548cSQLite\uff09\u5c0dBLOB\u5b57\u6bb5\u7684\u8655\u7406\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"mysql%e4%b8%ad%e7%9a%84blob%e5%ad%97%e6%ae%b5-YQQmcXqtGK\">MySQL\u4e2d\u7684BLOB\u5b57\u6bb5<\/h2>\n<p>\u5728MySQL\u4e2d\uff0cBLOB\u5b57\u6bb5\u53ef\u4ee5\u5b58\u5132\u6700\u591a65,535\u5b57\u7bc0\u7684\u4e8c\u9032\u5236\u6578\u64da\u3002MySQL\u63d0\u4f9b\u4e86\u56db\u7a2e\u4e0d\u540c\u985e\u578b\u7684BLOB\u5b57\u6bb5\uff1aTINYBLOB\u3001BLOB\u3001MEDIUMBLOB\u548cLONGBLOB\uff0c\u5206\u5225\u5c0d\u61c9\u4e0d\u540c\u7684\u5b58\u5132\u5bb9\u91cf\u3002<\/p>\n<h3 id=\"%e5%89%b5%e5%bb%ba%e8%a1%a8%e6%a0%bc-YQQmcXqtGK\">\u5275\u5efa\u8868\u683c<\/h3>\n<pre><code>CREATE TABLE images (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    image_data BLOB NOT NULL\n);<\/code><\/pre>\n<h3 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-YQQmcXqtGK\">\u63d2\u5165\u6578\u64da<\/h3>\n<pre><code>INSERT INTO images (image_data) VALUES (?);<\/code><\/pre>\n<p>\u5728\u63d2\u5165\u6578\u64da\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\u4f86\u907f\u514dSQL\u6ce8\u5165\u653b\u64ca\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PHP\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$mysqli = new mysqli(\"localhost\", \"user\", \"password\", \"database\");\n$stmt = $mysqli-&gt;prepare(\"INSERT INTO images (image_data) VALUES (?)\");\n$stmt-&gt;bind_param(\"b\", $imageData);\n$imageData = file_get_contents(\"path\/to\/image.jpg\");\n$stmt-&gt;execute();\n?&gt;<\/code><\/pre>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-YQQmcXqtGK\">\u67e5\u8a62\u6578\u64da<\/h3>\n<pre><code>SELECT image_data FROM images WHERE id = ?;<\/code><\/pre>\n<p>\u67e5\u8a62\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528PHP\u7684`header`\u51fd\u6578\u4f86\u986f\u793a\u5716\u7247\uff1a<\/p>\n<pre><code>&lt;?php\n$stmt = $mysqli-&gt;prepare(\"SELECT image_data FROM images WHERE id = ?\");\n$stmt-&gt;bind_param(\"i\", $id);\n$stmt-&gt;execute();\n$stmt-&gt;bind_result($imageData);\n$stmt-&gt;fetch();\nheader(\"Content-Type: image\/jpeg\");\necho $imageData;\n?&gt;<\/code><\/pre>\n<h2 id=\"postgresql%e4%b8%ad%e7%9a%84blob%e5%ad%97%e6%ae%b5-YQQmcXqtGK\">PostgreSQL\u4e2d\u7684BLOB\u5b57\u6bb5<\/h2>\n<p>PostgreSQL\u4f7f\u7528`BYTEA`\u6578\u64da\u985e\u578b\u4f86\u5b58\u5132\u4e8c\u9032\u5236\u6578\u64da\u3002\u8207MySQL\u4e0d\u540c\uff0cPostgreSQL\u4e0d\u9700\u8981\u7279\u5b9a\u7684BLOB\u985e\u578b\uff0c`BYTEA`\u53ef\u4ee5\u5b58\u5132\u4efb\u610f\u5927\u5c0f\u7684\u4e8c\u9032\u5236\u6578\u64da\u3002<\/p>\n<h3 id=\"%e5%89%b5%e5%bb%ba%e8%a1%a8%e6%a0%bc-YQQmcXqtGK\">\u5275\u5efa\u8868\u683c<\/h3>\n<pre><code>CREATE TABLE images (\n    id SERIAL PRIMARY KEY,\n    image_data BYTEA NOT NULL\n);<\/code><\/pre>\n<h3 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-YQQmcXqtGK\">\u63d2\u5165\u6578\u64da<\/h3>\n<pre><code>INSERT INTO images (image_data) VALUES (decode(?, 'escape'));<\/code><\/pre>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528PHP\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$conn = pg_connect(\"host=localhost dbname=database user=user password=password\");\n$imageData = file_get_contents(\"path\/to\/image.jpg\");\n$imageData = pg_escape_bytea($imageData);\npg_query_params($conn, \"INSERT INTO images (image_data) VALUES ($1)\", array($imageData));\n?&gt;<\/code><\/pre>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-YQQmcXqtGK\">\u67e5\u8a62\u6578\u64da<\/h3>\n<pre><code>SELECT image_data FROM images WHERE id = $1;<\/code><\/pre>\n<p>\u67e5\u8a62\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528PHP\u7684`header`\u51fd\u6578\u4f86\u986f\u793a\u5716\u7247\uff1a<\/p>\n<pre><code>&lt;?php\n$result = pg_query_params($conn, \"SELECT image_data FROM images WHERE id = $1\", array($id));\n$row = pg_fetch_assoc($result);\nheader(\"Content-Type: image\/jpeg\");\necho pg_unescape_bytea($row['image_data']);\n?&gt;<\/code><\/pre>\n<h2 id=\"sqlite%e4%b8%ad%e7%9a%84blob%e5%ad%97%e6%ae%b5-YQQmcXqtGK\">SQLite\u4e2d\u7684BLOB\u5b57\u6bb5<\/h2>\n<p>SQLite\u4e5f\u652f\u6301BLOB\u6578\u64da\u985e\u578b\uff0c\u4e26\u4e14\u53ef\u4ee5\u5b58\u5132\u4efb\u610f\u5927\u5c0f\u7684\u4e8c\u9032\u5236\u6578\u64da\u3002SQLite\u7684BLOB\u5b57\u6bb5\u4f7f\u7528`INSERT`\u8a9e\u53e5\u76f4\u63a5\u63d2\u5165\u4e8c\u9032\u5236\u6578\u64da\u3002<\/p>\n<h3 id=\"%e5%89%b5%e5%bb%ba%e8%a1%a8%e6%a0%bc-YQQmcXqtGK\">\u5275\u5efa\u8868\u683c<\/h3>\n<pre><code>CREATE TABLE images (\n    id INTEGER PRIMARY KEY AUTOINCREMENT,\n    image_data BLOB NOT NULL\n);<\/code><\/pre>\n<h3 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-YQQmcXqtGK\">\u63d2\u5165\u6578\u64da<\/h3>\n<pre><code>INSERT INTO images (image_data) VALUES (?);<\/code><\/pre>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528PHP\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$db = new SQLite3('database.db');\n$imageData = file_get_contents(\"path\/to\/image.jpg\");\n$stmt = $db-&gt;prepare(\"INSERT INTO images (image_data) VALUES (:image_data)\");\n$stmt-&gt;bindValue(':image_data', $imageData, SQLITE3_BLOB);\n$stmt-&gt;execute();\n?&gt;<\/code><\/pre>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-YQQmcXqtGK\">\u67e5\u8a62\u6578\u64da<\/h3>\n<pre><code>SELECT image_data FROM images WHERE id = :id;<\/code><\/pre>\n<p>\u67e5\u8a62\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528PHP\u7684`header`\u51fd\u6578\u4f86\u986f\u793a\u5716\u7247\uff1a<\/p>\n<pre><code>&lt;?php\n$stmt = $db-&gt;prepare(\"SELECT image_data FROM images WHERE id = :id\");\n$stmt-&gt;bindValue(':id', $id, SQLITE3_INTEGER);\n$result = $stmt-&gt;execute();\n$row = $result-&gt;fetchArray(SQLITE3_ASSOC);\nheader(\"Content-Type: image\/jpeg\");\necho $row['image_data'];\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-YQQmcXqtGK\">\u7e3d\u7d50<\/h2>\n<p>\u4e0d\u540c\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u5c0dBLOB\u5b57\u6bb5\u7684\u8655\u7406\u65b9\u5f0f\u5404\u6709\u7279\u9ede\uff0cMySQL\u3001PostgreSQL\u548cSQLite\u90fd\u63d0\u4f9b\u4e86\u76f8\u61c9\u7684\u6578\u64da\u985e\u578b\u548c\u64cd\u4f5c\u65b9\u6cd5\u3002\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u548c\u6b63\u78ba\u7684\u64cd\u4f5c\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u5b58\u5132\u4e8c\u9032\u5236\u6578\u64da\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u5b58\u5132\u548c\u8655\u7406\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u5c07\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u4e0d\u540c\u6578\u64da\u5eab\u5c0dBLOB\u5b57\u6bb5\u7684\u8655\u7406\u65b9\u6cd5\uff0c\u63d0\u4f9b\u4ee3\u78bc\u793a\u4f8b\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u7406\u89e3\u6578\u64da\u5b58\u5132\u8207\u64cd\u4f5c\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-152245","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/152245","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=152245"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/152245\/revisions"}],"predecessor-version":[{"id":152246,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/152245\/revisions\/152246"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=152245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=152245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=152245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}