Insert file into SQL field and extract back into file

时间:2019-04-16 23:30:13

标签: mysql file

I created simple table:

SHOW DATABASES;
CREATE DATABASE observe;

USE observe;

CREATE TABLE `see_me` (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
name VARCHAR(20),
food VARCHAR(30),
confirmed CHAR(1), 
signup_date DATE,
file_contents LONGBLOB // or VARBINARY(MAX)
);

and a file called t.txt. How can I insert the file into the field file_contents with sql commands and extract it likewise into a file?

The insert may be something like

 INSERT INTO see_me(id, file_contents) 
 SELECT '1', BulkColumn FROM OPENROWSET (BULK '/Users/usr_00/lab/t.txt', SINGLE_BLOB) as ...;

Can someone advise?

3 个答案:

答案 0 :(得分:3)

您可以使用帮助LOAD_FILE功能将文件内容插入到字段中。例如:

INSERT INTO see_me (file_contents)
VALUES (LOAD_FILE('/var/lib/mysql-files/myfile.txt'));

或在更新查询中

UPDATE see_me
SET file_contents = LOAD_FILE('/var/lib/mysql-files/myfile.txt');

如果要将文件内容提取到新文件中,可以使用SELECT .. INTO DUMPFILE。例如:

SELECT file_contents INTO DUMPFILE '/var/lib/mysql-files/myfile.txt'
FROM see_me
WHERE id = <your_id>;

但是,正如@VonC所说,请记住,如果secure_file_priv系统变量是非空的,则应使用仅位于已定义目录中的文件

答案 1 :(得分:2)

尝试以in here的身份,以specified in the manual的身份检查MySql在何处具有上载访问权限:

select @@secure_file_priv;
  

如果secure_file_priv system variable设置为非空目录名称,则要加载的文件必须位于该目录中。
  (在MySQL 8.0.17之前,该文件必须所有人都可读,而不仅仅是服务器可读。)

在Windows上,您必须像show in this answer那样逃避\

答案 2 :(得分:2)

大量工作后,以下内容对我有用。首先,我们必须确保我们是否具有从特定目录读取文件的适当权限。对我而言,它不存在。所以我改变了一些文件。

  1. 我去了这个位置( C:\ ProgramData \ MySQL \ MySQL Server 8.0 \ my.ini ),并检查了路径 secure-file-priv =“ C: / ProgramData / MySQL / MySQL Server 8.0 / Uploads” ,在此位置,我创建了一个文件“ check.txt”,其中包含内容。

  2. 我去了mysql workbench并检查了一些属性。运行以下命令

    像“ local_infile”一样显示全球变量;

如果它显示为OFF,则必须使用以下命令将其打开。

SET GLOBAL local_infile=1;
  1. 现在让我们检查 secure-file-priv 的目录,它应该'C:/ ProgramData / MySQL / MySQL Server 8.0 / Uploads / uploads'运行以下命令

    类似于“ secure_file_priv”的显示变量//您将看到路径为“ C:/ ProgramData / MySQL / MySQL Server 8.0 / Uploads / uploads”

现在,我们更改了属性。现在我们可以使用mysql读取文件并创建文件了。

  1. 从txt文件读取数据(我已经在“ C:/ ProgramData / MySQL / MySQL Server 8.0 / Uploads”中创建了 check.txt )。后来创建了一个临时表,我将数据存储在其中的check.txt

    CREATE TEMPORARY TABLE foo (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,content 
    longblob); // created temporary table called "foo"
    
    // reading the data from txt file
    
    LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/check.txt'
    INTO TABLE foo 
    #FIELDS TERMINATED BY '' ENCLOSED BY '' ESCAPED BY ''
    LINES STARTING BY '' TERMINATED BY '' // mysql creates new record for every line
    (content); 
    
     //by running above command entire txt data came to content column (mysql created only 1 row bcz we didn't mention lines terminated by. So it will copy entire txt data in 1st row of "content" column of "foo" table)
    
  2. 将内容写入文件(在“ C:/ ProgramData / MySQL / MySQL Server 8.0 / Uploads”目录中)

    SELECT * FROM foo
    INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/out.txt'; // out.txt will automatically created by mysql
    

通过运行以上命令,您可以执行读取和写入操作。您可以在“ see_me”表中的“ foo”表数据上方使用并执行读取或写入操作。

data from "foo" table where id="1" into "see_me" table (file_contents)

以下链接对我有所帮助。

mysql bugs documentation

setting secure-file-priv in mac and ubuntu

my.cnf file in mac

my-cnf location