LOAD_FUNCTION in MySQL shows an invalid identifier - mysql

I created a table namely pictures to insert images into the table. After using the insert command and load my picture with load_function it's showing an invalid identifier.
CREATE table pics(id number(10),pics1 blob);
INSERT INTO pics
values(1,LOAD_FUNCTION('C:\Users\Vandana\Desktop\face\my_image.jpg'));

Your issue is caused by your CREATE TABLE statement.
number is not a valid MySQL column data type, you should use INTEGER(10) SIGNED [sic].
CREATE TABLE pics(id INTEGER(10) SIGNED, pics1 BLOB);
However NUMBER is a valid data type for Oracle Database, which is very much so different from MySQL.
LOAD_FUNCTION is not a valid MySQL function [sic].
I believe you are wanting LOAD_FILE, which will retrieve the file's contents for the blob column.
You should also use double backslashes when referencing Windows file paths, to avoid escape sequence characters like \n.
INSERT INTO pics(id, pics1)
VALUES(1, LOAD_FILE('C:\\Users\\Vandana\\Desktop\\face\\my_image.jpg'));
Please keep in mind that LOAD_FILE only has access to the files
located on the system and that the user/group running the database service has read permissions for.
See secure_file_priv for more details.

In MySQL is better to use LOAD_FILE command:
MySQL LOAD_FILE() reads the file and returns the file contents as a string, which in your case will be a blob
CREATE table pics(id number(10),pics1 blob);
INSERT INTO pics
values(1, LOAD_FILE('C:\Users\Vandana\Desktop\face\my_image.jpg'));
Remembering: You need the right read privileges on your file.
You can refer:
More complete answer with load_file()

Related

How to store files in MySQL database using MySQL Workbench?

I'm trying to store .html files in a MySQL database (using MySQL Workbench). At the moment I'm trying to do load_file('filepath') so the contents of the .html file can be read and converted to string and that string will be stored in the table in the DB. But the problem is that it seems to always return null so I can't check if it's actually working.
I know there are 4 criteria to pass for load_file to work but I don't know which scripts I have to execute to grant the privileges. Since I'm trying to convert the contents of the file to string, maybe there is another way more efficient to store the whole .html file into the DB and if yes could anyone show me how to do this? My MySQL server has a username of root and working on Mac OS.
The load_file function says that:
To use this function, the file must be located on the server host, you must specify the full path name to the file, and you must have the FILE privilege. The file must be readable by all and its size less than max_allowed_packet bytes. If the secure_file_priv system variable is set to a nonempty directory name, the file to be loaded must be located in that directory.
So there are a number of conditions to check if you actually can load the file. Start by checking if your user has the FILE privilege and that the file is accessible by your MySQL server.
For people who's looking for, this is how I did it.
I have a table as
CREATE TABLE `test` (
`ID` int(11) DEFAULT NULL,
`UPLOAD_FILE` mediumblob
);
and I save a file smiley.jpg as
INSERT INTO test (`ID`, `UPLOAD_FILE`) VALUES ('1', LOAD_FILE('E:/smiley.jpg'));

MYSQL csv filename to table

Is it possible to search a folder and grab a file name to convert into a mysql table? Can it be done in a query or stored procedures?
I'm new to mysql and so far I can read my data and create a table which is manually done, but don't know how to create a table with its file name automatically.
LOAD DATA
LOCAL INFILE 'D:/test.csv'
INTO TABLE test
Unfortunately it's not possible for LOAD DATA to create the table for you. The syntax for the function goes as ...
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
**INTO TABLE tbl_name**
The table name has to be specified, which means you have to create the table first.
Why is it so?
There are so many different data types that are supported in a mysql table that the importer is unable to judge for itself which data type to use.
Fro example if the first line in the CSV was
hello, 1
The importer might choose CHAR(5) and INT but the next line might be
Colombo, Galle
This line cannot be imported into CHAR(5) or INT
You have tagged your question with mysql-workbench, even though you don't ask about this tool. But let me tell that MySQL Workbench indeed can be of help here. In the SQL IDE which opens when you click on a connection tile on the homescreen you can use the context menu in the sidebar to open the table data import wizard. This will help you to import CSV (or JSON) data from a file and will also create the target table for you (if you don't pick an existing one).

Load and replace file path string with the content from that file in a MySQL database

I have a database of entries consisting of a 'name', 'id' and a 'description', but currently the 'description' field is set to the file path of a .txt file that actually contains the description content. Each .txt file's name is each row's 'id', plus the .txt extension and they all reside in the same directory.
Can I load and replace each 'description' field with the content from the relevant text file (using MySQL)?
You can't write a MySQL query directly that will read the description values from your file system. That would require the MySQL server to be able to read raw text from files in your file system. You Can't Do Thatâ„¢.
You certainly can write a program in your favorite host language (php, java, PERL, you name it) to read the rows from your database, and update your description rows.
You could maybe contrive to issue a LOAD DATA INFILE command to read each text file. But the text files would have to be very carefully formatted to resemble CSV or TSV files.
Purely using mysql this would be a difficult, if not impossible exercise because mysql does not really offer any means to open files.
The only way to open an external text file from mysql is to use LOAD DATA INFILE command, which imports the text file into a mysql table. What you can do is to write a stored procedure in mysql that:
Create a temporary table with a description field large enough to accommodate all descriptions.
Reads all id and description field contents into a cursor from your base table.
Loop through the cursor and use load data infile to load the given text file's data into your temporary table. This is where things can go wrong. The account under which mysql daemon / service runs needs to have access to the directories and fiels where the description files are stored. You also need to be able to parametrise the load data infile command to read the full contents of the text file into a single field, so you need to set the field and line terminated by parameters to such values that cannot be found in any of the description files. But, even for this you need to use a native UDF (user defined function) that can execute command line programs because running load data infile directly from stored procedures is not allowed.
See Using LOAD DATA INFILE with Stored Procedure Workaround-MySQL for full description how to this.
Issue an update statement using the id from the cursor to update the description field in your base table from the temporary table.
Delete the record from your temp table.
Go to 3.
It may be a lot easier to achieve this from an external programming language, that has better file manipulation functions and can update each record in your base table accordingly.

MySQL Writing Image BLOB To Disk

I have a table that contains image BLOB field.
I want to be able to submit a query to the database and have the BLOBs written to the windows file system.
Is this possible??
Yes, it is possible. You can use SELECT command with INTO DUMPFILE clause. For example -
SELECT
data_column
FROM
table1
WHERE
id = 1
INTO DUMPFILE 'image.png';
From the reference: If you use INTO DUMPFILE instead of INTO OUTFILE, MySQL writes only one row into the file, without any column or line termination and without performing any escape processing. This is useful if you want to store a BLOB value in a file.

Create MySQL table from xls spreadsheet

I wonder if there is a (native) possibility to create a MySQL table from an .xls or .xlsx spreadsheet. Note that I do not want to import a file into an existing table with LOAD DATA INFILE or INSERT INTO, but to create the table from scratch. i.e using the header as columns (with some default field type e.g. INT) and then insert the data in one step.
So far I used a python script to build a create statement and imported the file afterwards, but somehow I feel clumsy with that approach.
There is no native MySQL tool that does this, but the MySQL PROCEDURE ANALYSE might help you suggest the correct column types.
With a VB Script you could do that. At my client we have a script which takes the worksheet name, the heading names and the field formats and generates a SQL script containing a CREATE TABLE and a the INSERT INTO statements. We use Oracle but mySQL is the same principle.
Of course you could do it even more sophisticated by accessing mySQL from Excel by ODBC and post the CREATE TABLE and INSERT INTO statements that way.
I cannot provide you with the script as it is the belonging of my client but I can answer your questions on how to write such a script if you want to write one.