Friday, April 20, 2007

MySQL - Import files into database with LOAD_FILE()

LOAD_FILE(file_name)
Reads the file and returns the file contents as a string. To use this function, the file must be located on the server host, you must specify the full pathname to the file, and you must have the FILE privilege.

Example1:

UPDATE t
SET blob_col=LOAD_FILE('/tmp/picture')
WHERE id=1;

Example2:

use rgr;
create table netleih1 (fn varchar(255) not null primary key,data text);
insert into netleih1(fn,data) select 'title_34504.html', LOAD_FILE('Z:/Downloads/netleih-detail/title_34504.html');
select * from netleih1;