How do I use LOAD_FILE to insert value from a file

2019-08-16 19:41发布

问题:

I tried to insert a text file into my database, using this code:

INSERT INTO test.table (url_address, html)
VALUES ('abc', LOAD_FILE('C:\Documents and Settings\eran\Desktop\1.txt'));

However, I get null in the html column. How can I get the data from the text file to my database?

回答1:

MySQL LOAD_FILE() reads the file and returns the file contents as a string.

To use this function, the file must be located on the host server, user must specify the full path name of the file, and user must have the FILE privilege. The file must be readable and size must be less than max_allowed_packet (set in the my.ini file) bytes. It returns NULL if the file does not exist or can’t be read.

Try using double back slash "\" for the path. I do not know if it will work.

http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_load-file http://www.w3resource.com/mysql/string-functions/mysql-load_file-function.php