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?
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.
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.
INSERT INTO Syntax Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With