Discussione:
MySql: come inserire un BLOB
(troppo vecchio per rispondere)
Enrico
2004-07-06 06:38:37 UTC
Permalink
Ciao a tutti,

sto usando MySql per un progetto. Ho creato una tabella che ha un
campo di tipo BLOB perche' ci devo mettere un immagine.
Non ho idea di come possa essere la query per inserire effettivamente
l'immagine. Qualcuno mi puo' aiutare?

grazie
Enrico
Paolo casa
2004-07-06 12:10:53 UTC
Permalink
Post by Enrico
sto usando MySql per un progetto. Ho creato una tabella che ha un
campo di tipo BLOB perche' ci devo mettere un immagine.
Non ho idea di come possa essere la query per inserire effettivamente
l'immagine. Qualcuno mi puo' aiutare?
Con quale linguaggio stai sviluppando l'applicativo per interfacciarti al
database ?
Se mi dai più info, forse posso aiutarti.
Post by Enrico
grazie
Enrico
prego

Ciao
Paolo Ardissone
Enrico
2004-07-06 14:19:15 UTC
Permalink
Post by Paolo casa
Post by Enrico
sto usando MySql per un progetto. Ho creato una tabella che ha un
campo di tipo BLOB perche' ci devo mettere un immagine.
Non ho idea di come possa essere la query per inserire effettivamente
l'immagine. Qualcuno mi puo' aiutare?
Con quale linguaggio stai sviluppando l'applicativo per interfacciarti al
database ?
Se mi dai più info, forse posso aiutarti.
Ciao
Paolo Ardissone
Ciao,

devo scrivere una query in SQL. Ho visto sulla documentazione di
PostgreSQL che
la sintassi per caricare un binario e' qualcosa del tipo:

test=> CREATE TABLE fruit (name CHAR(30), image OID);
test=> INSERT INTO fruit
test=> VALUES ('peach', lo_import('/usr/images/peach.jpg'));


mentre per leggere e':

test=> SELECT lo_export(fruit.image, '/tmp/outimage.jpg')
test=> FROM fruit
test-> WHERE name = 'peach';

Stavo cercando un comando simile per MySQL

ciao,
Enrico
gmax
2004-07-06 18:04:29 UTC
Permalink
Enrico wrote:
[SNIP]
Post by Enrico
devo scrivere una query in SQL. Ho visto sulla documentazione di
PostgreSQL che
test=> CREATE TABLE fruit (name CHAR(30), image OID);
test=> INSERT INTO fruit
test=> VALUES ('peach', lo_import('/usr/images/peach.jpg'));
test=> SELECT lo_export(fruit.image, '/tmp/outimage.jpg')
test=> FROM fruit test-> WHERE name = 'peach';
Stavo cercando un comando simile per MySQL
ciao,
Enrico
La funzione che cerchi e' LOAD_FILE

http://dev.mysql.com/doc/mysql/en/String_functions.html

LOAD_FILE(file_name)
Reads the file and returns the file contents as a string. The file
must be located on the server, you must specify the full pathname to the
file, and you must have the FILE privilege. The file must be readable by
all and be smaller than max_allowed_packet bytes. If the file doesn't
exist or cannot be read because one of the preceding conditions is not
satisfied, the function returns NULL.

mysql> UPDATE tbl_name
SET blob_column=LOAD_FILE('/tmp/picture')
WHERE id=1;

Before MySQL 3.23, you must read the file inside your application
and create an INSERT statement to update the database with the file
contents. If you are using the MySQL++ library, one way to do this can
be found in the MySQL++ manual, available at http://dev.mysql.com/doc/.



Ciao

gmax
--
____ ____ _____ _ _
/ _ | \(____ ( \ / )
( (_| | | | / ___ |) X (
\___ |_|_|_\_____(_/ \_)
(_____|
Sapere, saper fare, fare, far sapere
http://gmax.oltrelinux.com
Enrico
2004-07-07 05:49:11 UTC
Permalink
Post by gmax
[SNIP]
Post by Enrico
devo scrivere una query in SQL. Ho visto sulla documentazione di
PostgreSQL che
test=> CREATE TABLE fruit (name CHAR(30), image OID);
test=> INSERT INTO fruit
test=> VALUES ('peach', lo_import('/usr/images/peach.jpg'));
test=> SELECT lo_export(fruit.image, '/tmp/outimage.jpg')
test=> FROM fruit test-> WHERE name = 'peach';
Stavo cercando un comando simile per MySQL
ciao,
Enrico
La funzione che cerchi e' LOAD_FILE
http://dev.mysql.com/doc/mysql/en/String_functions.html
LOAD_FILE(file_name)
Reads the file and returns the file contents as a string. The file
must be located on the server, you must specify the full pathname to
the file, and you must have the FILE privilege. The file must be
readable by all and be smaller than max_allowed_packet bytes. If the
file doesn't exist or cannot be read because one of the preceding
conditions is not satisfied, the function returns NULL.
mysql> UPDATE tbl_name
SET blob_column=LOAD_FILE('/tmp/picture')
WHERE id=1;
Before MySQL 3.23, you must read the file inside your application
and create an INSERT statement to update the database with the file
contents. If you are using the MySQL++ library, one way to do this can
be found in the MySQL++ manual, available at http://dev.mysql.com/doc/.
Ciao
gmax
Grazie!

l'ho provata e funziona bene.

Ci sono solo due problemi che pero' sono aggirabili:

1. LOAD_FILE( ) vuole un pathname assoluto

2. non e' simmetrico l'inserimento e la lettura del file (come in
PostgreSQL):
mi piacerebbe, quando leggo il file dal DB, potergli passare un
pathname e avere il DB che lo scrive dove gli ho indicato.
Invece non
e' cosi', cioe' ritorna il file come uno stream di dati sulla
linea di comando.

Sembra pero' lavorare bene con i file binari.

ciao e grazie
Enrico
Enrico
2004-07-07 05:49:47 UTC
Permalink
Post by gmax
[SNIP]
Post by Enrico
devo scrivere una query in SQL. Ho visto sulla documentazione di
PostgreSQL che
test=> CREATE TABLE fruit (name CHAR(30), image OID);
test=> INSERT INTO fruit
test=> VALUES ('peach', lo_import('/usr/images/peach.jpg'));
test=> SELECT lo_export(fruit.image, '/tmp/outimage.jpg')
test=> FROM fruit test-> WHERE name = 'peach';
Stavo cercando un comando simile per MySQL
ciao,
Enrico
La funzione che cerchi e' LOAD_FILE
http://dev.mysql.com/doc/mysql/en/String_functions.html
LOAD_FILE(file_name)
Reads the file and returns the file contents as a string. The file
must be located on the server, you must specify the full pathname to
the file, and you must have the FILE privilege. The file must be
readable by all and be smaller than max_allowed_packet bytes. If the
file doesn't exist or cannot be read because one of the preceding
conditions is not satisfied, the function returns NULL.
mysql> UPDATE tbl_name
SET blob_column=LOAD_FILE('/tmp/picture')
WHERE id=1;
Before MySQL 3.23, you must read the file inside your application
and create an INSERT statement to update the database with the file
contents. If you are using the MySQL++ library, one way to do this can
be found in the MySQL++ manual, available at http://dev.mysql.com/doc/.
Ciao
gmax
Grazie!

l'ho provata e funziona bene.

Ci sono solo due problemi che pero' sono aggirabili:

1. LOAD_FILE( ) vuole un pathname assoluto

2. non e' simmetrico l'inserimento e la lettura del file (come in
PostgreSQL):
mi piacerebbe, quando leggo il file dal DB, potergli passare un
pathname e avere il DB che lo scrive dove gli ho indicato.
Invece non
e' cosi', cioe' ritorna il file come uno stream di dati sulla
linea di comando.

Sembra pero' lavorare bene con i file binari.

ciao e grazie
Enrico

Continua a leggere su narkive:
Loading...