在mysql中存储二进制数据

发布于 2024-07-26 16:05:42 字数 374 浏览 2 评论 0原文

我的本地计算机上有一个 PDF 文件。 我想将此文件上传到 SQL 数据库上的 BINARY BLOB。 这里提到的其他方法 [MySQL 中的二进制数据都使用 PHP。 我想要一种简单干净的方法在 Linux 命令行上上传此 PDF 文件。 不幸的是,我无权访问远程文件系统,因此不能像其他地方提到的那样只存储文件的链接...我有点需要使用这个 MySQL 数据库作为这些 PDF 文件的虚拟文件系统。

从 PhP 示例中,它似乎所需要做的就是在使用 INSERT 命令之前转义斜杠? 有没有一种简单的方法可以在 Linux 命令行上实现这一点?

I have a PDF file on a local machine. I want to upload this file into a BINARY BLOB on a SQL database. Other approaches mentioned here [Binary Data in MySQL all use PHP. I want a simple clean way to upload this PDF file on the Linux command line. Unfortunately, I do not have access to the remote filesystem so cannot just store links to the file as mentioned elsewhere... I sort of need to use this MySQL database as a virtual filesystem for these PDF files..

From the PhP example, it seems all that is required is to escape the slashes before using the INSERT command? Is there a simple way to achieve that on a Linux command-line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浸婚纱 2024-08-02 16:05:43

您可以使用 mysql 函数 LOAD_FILE我想结合一个小的 shellscript 来做到这一点。

未经测试的代码如下:

#!/bin/bash

if [ -z $1 ]
then 
    echo "usage: insert.sh <filename>"
else
    SQL="INSERT INTO file_table (blob_column, filename) VALUES(LOAD_FILE('$1'), '$1')"

    echo "$SQL" > /tmp/insert.sql
   cat /tmp/insert.sql | mysql -u user -p -h localhost db
fi

您可以像这样使用它:

<prompt>./insert.sh /full/path/to/file

通过错误检查、正确的临时文件创建、转义和其他细节更好地实现,作为练习留给读者。 请注意,使用LOAD_FILE()需要MySQL中的FILE权限和文件的完整路径。

You could use the mysql function LOAD_FILE in conjunction with a small shellscript to do this I guess.

Untested code follows:

#!/bin/bash

if [ -z $1 ]
then 
    echo "usage: insert.sh <filename>"
else
    SQL="INSERT INTO file_table (blob_column, filename) VALUES(LOAD_FILE('$1'), '$1')"

    echo "$SQL" > /tmp/insert.sql
   cat /tmp/insert.sql | mysql -u user -p -h localhost db
fi

And you could use it like this:

<prompt>./insert.sh /full/path/to/file

Better implementation with error checking, proper tempfile creation, escaping and other niceties is left as an exercise to the reader. Note that use of LOAD_FILE() requires the FILE privilege in MySQL and a full path the file.

堇年纸鸢 2024-08-02 16:05:43

您可以使用curl 浏览器提交与GUI 浏览器相同的POST。 嗅探 GUI 浏览器发送的请求,然后使用curl 复制该请求。

You could use the curl browser to submit the same POST that your GUI browser does. Sniff the request that your GUI browser sends, then replicate that with curl.

帅气尐潴 2024-08-02 16:05:43

不确定这是否完全解决了我的问题,但我发现了一个在 MySQL 之上实现的文件系统层。 我想我可以用它来自动将我的 PDF 文件存储为 BLOB...仍然需要弄清楚如何在此文件系统中存储 PDF 文件的密钥,以便基于比 < 更有意义的东西对 PDF 文件进行结构化访问。 索引节点,序列>。

http://sourceforge.net/projects/mysqlfs/
http://www.linux.com/archive/feature/127055

Not sure if this completely solves my problem, but I found a filesystem layer implemented on top of MySQL. I guess I can use that to store my PDF files as BLOBs automatically... still need to figure out how to store keys to the PDF file in this filesystem for structured access to the PDF file based on something more meaningful than < inode,seq >.

http://sourceforge.net/projects/mysqlfs/ and
http://www.linux.com/archive/feature/127055

伴随着你 2024-08-02 16:05:43

请参阅 MySQL 说明书中的这篇文章,其中还包含示例脚本:

http://www.freeopenbook.com/mysqlcookbook/mysqlckbk-CHP-17-SECT-7.html

但它有一些限制,最值得注意的是该文件必须与 mysql 服务器存储在同一主机上。

See this article in the MySQL cookbook which includes a sample script as well:

http://www.freeopenbook.com/mysqlcookbook/mysqlckbk-CHP-17-SECT-7.html

It has some restrictions though, most notably that the file must be stored on the same host as the mysql server.

抱猫软卧 2024-08-02 16:05:43

她的版本没有 LOAD_FILE

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// Create a connection to the database
$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Read the file content into a variable
$file_content = file_get_contents('/path/to/file.txt');

// Escape the file content for use in SQL
$file_content_escaped = $conn->real_escape_string($file_content);

// SQL query
$sql = "INSERT INTO table_name (column_name) VALUES ('$file_content_escaped')";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

// Close the connection
$conn->close();
?>

MariaDB 手册 - LOAD_FILE()

注意 :使用 LOAD_FILE() 函数的语句对于基于语句的复制来说并不安全。 这是因为从机将自行执行 LOAD_FILE() 命令。 如果从属设备上不存在该文件,该函数将返回 NULL。

Her a version without LOAD_FILE

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// Create a connection to the database
$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Read the file content into a variable
$file_content = file_get_contents('/path/to/file.txt');

// Escape the file content for use in SQL
$file_content_escaped = $conn->real_escape_string($file_content);

// SQL query
$sql = "INSERT INTO table_name (column_name) VALUES ('$file_content_escaped')";
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

// Close the connection
$conn->close();
?>

MariaDB Manual - LOAD_FILE()

Note: Statements using the LOAD_FILE() function are not safe for statement based replication. This is because the slave will execute the LOAD_FILE() command itself. If the file doesn't exist on the slave, the function will return NULL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文