Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert file contents into MySQL table's column

Tags:

file

mysql

insert

I know there's the LOAD DATA INFILE statement, which allows me to INSERT structured data into a table.

What I'm curious about, is whether it is possible to INSERT contents of a file into single column. So, is something like this:

INSERT INTO my_table (stamp, what) VALUES (NOW(), LOAD DATA INFILE 'my_file');

possible?

like image 384
Martin Tóth Avatar asked Jun 23 '10 11:06

Martin Tóth


People also ask

How do I insert data into a specific column in MySQL?

In syntax, First, you must specify the name of the table. After that, in parenthesis, you must specify the column name of the table, and columns must be separated by a comma. The values that you want to insert must be inside the parenthesis, and it must be followed by the VALUES clause.

How do I insert data into a MySQL database from a text file?

mysql> LOAD DATA LOCAL INFILE '/path/pet. txt' INTO TABLE pet; If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead: mysql> LOAD DATA LOCAL INFILE '/path/pet.

How do I insert data into a specific column in SQL?

INSERT INTO Syntax Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)

How to import only specific columns from text file into MySQL?

How can we import only specific columns from the text file, into MySQL table? Suppose if we have the values for some specific columns in the text file and MySQL table, in which we want to import the data, is having an extra column (s) then by mentioning the names of the columns in the query we can upload the values of those specific columns only.

How to add data in MySQL tables using the INSERT statement?

Learn MySQL: Add data in tables using the INSERT statement. 1 Simple INSERT statement to add data to the table. 2 Use INSERT Statement to add multiple rows in the table. 3 INSERT INTO SELECT clause to insert the output generated by the SELECT query. 4 INSERT IGNORE clause to ignore the error generated during the execution of the query.

How do I load a file into a mySQL table?

You can use the LOAD_FILE () function: CREATE TABLE my_table (stamp datetime, what text); INSERT INTO my_table (stamp, what) VALUES (NOW (), LOAD_FILE ('/tmp/my_file.txt')); You'll have to make sure that the file is readable by MySQL, and that your MySQL user has the FILE privilege. This is the same privilege required for LOAD DATA INFILE.

How to store a file in a MySQL database table?

You know, to store a file into a database table, the table must have a column whose data type is BLOB ( Binary Large OBject ). Assuming we have a MySQL table called person which is created by the following SQL script: That means the photo column can store a file up to 16 MB. You can choose which blob type, depending on your need.


1 Answers

Yes it's possible. You can use the LOAD_FILE() function:

CREATE TABLE my_table (stamp datetime, what text);

INSERT INTO my_table (stamp, what) VALUES (NOW(), LOAD_FILE('/tmp/my_file.txt'));

You'll have to make sure that the file is readable by MySQL, and that your MySQL user has the FILE privilege. This is the same privilege required for LOAD DATA INFILE.

like image 115
Daniel Vassallo Avatar answered Oct 22 '22 22:10

Daniel Vassallo