PDA

View Full Version : INSERT INTO query for pictures data types


dblinux
05-06-02, 12:43
INSERT INTO "categories"("categoryid", "categoryname", "description", "picture") VALUES (1, 'Beverages', 'Soft drinks, coffees, teas, beers, and ales', ?/some strange char...);

This query don't run correctly. How can I do to run This?

eperich
05-06-02, 18:12
Do you want to store the picture as a binary in the database?

Then you should use BLOB

dblinux
05-07-02, 03:50
What is BLOB? A postgre Data type?
Another question? In the SQL Query to add the picture how can I do?
For Example...

INSERT INTO Authors(Name, Surname, Picture) VALUES ('John', 'Lucas', ...);

(...) What char:

[ ' ] For string
and for pictures???

eperich
05-07-02, 04:11
BLOB = Binary Large Object

Is a datatype in postgresql.

String you can take varchar
But I'm taking always text.

text costs more bytes than varchar but it has then no restrictions about the length.

One question:

What dou you store as a picture?

give me a real example?

I think you need blob
Look at this url

BLOB DOC (http://www.postgresql.org/idocs/index.php?datatype-binary.html)

dblinux
05-07-02, 05:23
This is my real Example:

I've got a Ms-Access Table to migrate in PostGreSQL.

In ms-access table there is a pictures field cotaining strange chars (the picture data).

Now... the query for PostGreSQL is:

CREATE TABLE categories(categoryid char(254), categoryname char(50), description char(50), picture bytea);

Is right???

The query for the migration is...

INSERT INTO "categories"("categoryid", "categoryname", "description", "picture") VALUES (1, 'Beverages', 'Soft drinks, coffees, teas, beers, and ales', '?/
!???????????????A? ? ???h ? ?? V ( ¬ x  ? ?   ?ÿ?ÿÿÿ ÿ? ? ÿ  ? A? a? aA ?A ???  ? A ? ? Ð ??  A ? Ð? ?aa A?? ?? a??????œ? AÐ? ? Ð  ? ?????????y?Aa? Ð ?? Ð ?? I?????????y? ? ? Ð ?A??Ð?   ???????????J? A A   ? ???? ???????Aaa??  aA  A?? ?????? A   ?aa! ?? ?????p???q ?aAa  ????u???? A???????????A?   @ ???A?????  Ð A ??g????????????o ????????????????  ? ????q?????????????%?????????w??????0?  A ??????????????????????????????????? ? A? a????????????????????????????????????? a ?????????????????????????????c????? ?A A ?????????????????????????????????????? ? ???????????????????????????Ð?????? ? a????v?????? ????????????????????????? AA ????g?????????????????????????????? ? a????F????????????????????????????!A??AA !????????????w???????????a ?????????I?? A??????????????????
??????????????????AÐ A AA????????????????????????????????????I?? ?????????????????????????????????·??AA ??A ?A?@A´??????????????????????y?-?s???Ð  ??????a ?t?????????????????????a ??  ? ?? ?????????????????????0AaA A? ?? ??` ???????????????k???????? ?a  A?0? ???? ????????????????e????we~?Ð A Ð?@f @ ??????????????????????´???a??P?p ?? ???????????????a?d???????e?g ??? ?@ ??????????????????????????š?Fd?g?E??a??w??@?? ??????????????????????????? ?????E???t?? ?? ?????????????????????????????_????????p ?v????????????????????????????e?????????????????d ????????????????????????e??????????????D????????? ??????????????????????????????p?p? dd????????????????????????????????????4??` @?????????????????????????????????????P??? ????w???????????????`???????????????@?w???@ ?g?w???????????????????????????????p?? ????D?d?p???????????????????????????????7?w??????v ? ???????????????????????????????? ?????????? ???????????????????????????????? A???????p? ? d???????????
????????????????W@ ????? ?? ????? ? ????????????????????3?D D ?? ?? ?????? ?????????????????????? g? ?? ? ?????? ??????????????????????e ?? ?? ????d???????????????????????????????????7 ??w ??????????????????????????????? ???t ?@ ??????????????????????????????? ???p ?`??????????-F???????????????????????? @ ??????????????????????????????????? `???????t?????????????????????????? F `???????0 A???????????????????????? ?? ?????????? ??????????????????????? ?? ?¯?????? ????????????????????????? ?@ ???????? ?JA???????????????????????? ? d?@ `???????? ???????????????????????q ?@?? ???????? ?? ??????????????????????p F??F  `?????? ?????????????????????????w ?? ?@`?????? ?????????????????????????? ??? ??????? ?????????????????????????p ??? ???????? `????????????????????????P ??D@ ???????? ????????????????????????? ???` ??????????????????????????????????? ???@ `????????f????????????????????????? ?? ?????????p?????????????
??????????? ?d? ?????????A????????????????????????? ?d `???????d?????????????????????????? ?@ ??????????????????????????????????? ? ??????????????????????????????????P ??` ?????????@????????????????????????p ???d ??????????????????????????????????? ???@ ??????????????????????????????????p? ??? ?????????????????????????????????p@ ??? ?w???????????????????????????????w ???D ?k????????????????????????????????$ ???f? ?5???w???????????????????????????W? ???d ??????D??????????????????????????? ?  ?@ ??????????????????????????????????@ ? ?? ??????????????????????????????????? ??@d ?????`???????????????????????????? ?? ?@ ?????????????????????????????????@ @??d ???w?????????????????????????+???? @ ??@ ???w?????????????????????????????u? F? ????v????????????????????????????s? ?` ??????????????????????????????????? ? ??????????????????????????????????? ? ? ???????????????????????????????????? ? ????????????????????????????????
???$ ??????????????????????????????????q?? ???????????????????????????????????@ ????v?????????????????????????????????@ ????p?????????????????????????????? ?@ ???????????????????????????????????r??@? ??? ????????????????????????????????A@?????????????? ??????????????????????????????  ????????????????????????????????????P? @? ????????????????????????????????????7???????????? ???????????????????????????????`?????????????????? ????????????????????????????????????????????????? ???????????????????? ?? @??????????????????????????????????????? ???????????????????????????????????????????? @?????????????????????????????????????????????u?? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ???????????a?????????????????????????????????????? ???????????????????????????????????????????????
?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????????????? ?????????????????????????????????????????? ? ??');

This query don't run correctly Message Error: Unterminated quoted String...

eperich
05-07-02, 05:56
ok
this picture data
is this a string or is this a real file

I mean did you made this string from a picture or do you have the string.

when you only have this string you must save this in text. and you MUST escape some characters.

Igf you have a file you only save the id from the BLOB in the table

I must look it uo. don't know it by heart.

dblinux
05-07-02, 06:07
What are the characters I must remove and what would be the Query to be executed correctly?

eperich
05-07-02, 06:51
as far as I know

are these

---'---
---\---

you must escape them
\'
\\

that must be all of them
the others have no effect

try to insert this picture data as a string!

eperich
05-07-02, 06:56
And you should write the insert statement this way:

INSERT INTO categories(categoryid, categoryname, description, picture) VALUES (1, 'Beverages', 'Soft drinks, coffees, teas, beers,......

Maybe this was the error
I don't have seen it til now

MaleMan
06-19-02, 03:43
what are advantages and disadvantages in using BLOB?
disk space is getting smaller after creating the database... thats what i see on it compared to linking files.

pls... can you guys make some suggestions, comments or wild reactions. :)

eperich
06-20-02, 18:46
My personal opinino is not to use BLOB before you don't really need it.

If you have huge amaount of data storing in a blob then think about it.

You cannot easily with BLOB's.
you must understand a bit of the handling.

Read the docs at postgresql and you will see

hachiman
11-19-02, 23:37
hi all there:

I was working a little around this subject and want to share my experiences with blobs and postgresql. This explanation is for PostgreSQL + Perl + DBI + DBD::Pg under Linux.
The version i'm using are:
postgres: 7.1.3
perl 5.6.0
DBI and DBD::Pg 1.30
Linux Red Hat 7.1 kernel 2.4.9

First, postgresql use a special data type to store blob named BYTEA
then, for the experiment, create the following table:

create table images (
id integer not null, -- or what ever you want
picture bytea
);

First step: inserting an image into the table: Create a test script,
named as you like:

#!/usr/bin/perl
use DBI qw/:sql_types/;

$dbh = DBI->connect("DBI:Pg:dbname=mybase");

DBI-> trace(3,'/tmp/dbierr'); ## use to trace all db activity

### copy an image in the same directory you use to create the script
## of course you can put it in every place you like, just point to it
###
my $imagen = "hotNtot-436d.gif";

### reading the file
##
open A, "<$imagen";
$img = "";
while(read(A, $data, 1024)) {
$img .= $data;
}
close A;

### now, inserting the image in the table:
#

## determining the max size we allow to store
##
$dbh->{LongReadLen} = 512 * 1024; ## 512 KB
$dbh->{LongTruncOk} = 1;

my $ID = 1; ### just for test

my $salvaimg = "INSERT INTO images VALUES ($ID, ?)";

my $sth = $dbh->prepare($salvaimg);

### this is the key to insert the blob
## here we are binding the blob, note that SQL_BINARY is
## defined in DBI qw/:sql_types/;
## and "$img" is the size in bytes of the image
## we got in the read loop:

$sth->bind_param(1, "$img", SQL_BINARY) || die $dbh->errstr;

$sth->execute($img); ## that it's !!!!
$dbh -> disconnect();
1;

Now, take a look in the /tmp/dbierr file and see very interesting stuff

Second Step: fetching the image.

In this step, i create a directory visible in the web, and grant write
permission to the web server user, in my case "nobody":

$ mkdir /var/www/html/images
$ chmod 775 /var/www/html/images
$ chgrp nobody /var/www/html/images

Now, the second script:

#!/usr/bin/perl
use DBI qw/:sql_types/;

$dbh = DBI->connect("DBI:Pg:dbname=mybase");
$dbh->{LongReadLen} = 512 * 1024;
$dbh->{LongTruncOk} = 1;

### use again to trace all db activity and for this you may erase
## the previous content of the /tmp/dbierr file
DBI-> trace(3,'/tmp/dbierr');

$sth = $dbh -> prepare("SELECT picture from images");
$sth -> execute();

$SALIDA = $sth -> fetchrow_array;

### recontructing the image
##
$IMA = "/var/www/html/images/recovered_image.gif";
open A, ">$IMA";
print A $SALIDA;
close A;

$dbh -> disconnect();
1;

Finally, open your browse and point to:

http://yourserver/images/recoverd_image.gif

With a litte patient, you can with easy transport this scripts into
the language you use and of course, apply them in more complex
situations.

I hoppe you find this helpfull.

hachiman

omprab
11-28-03, 08:32
hi,
This script works fine with dataype of bytea type but
fails for lo ,can u help in this

Regards
omkar

Originally posted by hachiman
hi all there:

I was working a little around this subject and want to share my experiences with blobs and postgresql. This explanation is for PostgreSQL + Perl + DBI + DBD::Pg under Linux.
The version i'm using are:
postgres: 7.1.3
perl 5.6.0
DBI and DBD::Pg 1.30
Linux Red Hat 7.1 kernel 2.4.9

First, postgresql use a special data type to store blob named BYTEA
then, for the experiment, create the following table:

create table images (
id integer not null, -- or what ever you want
picture bytea
);

First step: inserting an image into the table: Create a test script,
named as you like:

#!/usr/bin/perl
use DBI qw/:sql_types/;

$dbh = DBI->connect("DBI:Pg:dbname=mybase");

DBI-> trace(3,'/tmp/dbierr'); ## use to trace all db activity

### copy an image in the same directory you use to create the script
## of course you can put it in every place you like, just point to it
###
my $imagen = "hotNtot-436d.gif";

### reading the file
##
open A, "<$imagen";
$img = "";
while(read(A, $data, 1024)) {
$img .= $data;
}
close A;

### now, inserting the image in the table:
#

## determining the max size we allow to store
##
$dbh->{LongReadLen} = 512 * 1024; ## 512 KB
$dbh->{LongTruncOk} = 1;

my $ID = 1; ### just for test

my $salvaimg = "INSERT INTO images VALUES ($ID, ?)";

my $sth = $dbh->prepare($salvaimg);

### this is the key to insert the blob
## here we are binding the blob, note that SQL_BINARY is
## defined in DBI qw/:sql_types/;
## and "$img" is the size in bytes of the image
## we got in the read loop:

$sth->bind_param(1, "$img", SQL_BINARY) || die $dbh->errstr;

$sth->execute($img); ## that it's !!!!
$dbh -> disconnect();
1;

Now, take a look in the /tmp/dbierr file and see very interesting stuff

Second Step: fetching the image.

In this step, i create a directory visible in the web, and grant write
permission to the web server user, in my case "nobody":

$ mkdir /var/www/html/images
$ chmod 775 /var/www/html/images
$ chgrp nobody /var/www/html/images

Now, the second script:

#!/usr/bin/perl
use DBI qw/:sql_types/;

$dbh = DBI->connect("DBI:Pg:dbname=mybase");
$dbh->{LongReadLen} = 512 * 1024;
$dbh->{LongTruncOk} = 1;

### use again to trace all db activity and for this you may erase
## the previous content of the /tmp/dbierr file
DBI-> trace(3,'/tmp/dbierr');

$sth = $dbh -> prepare("SELECT picture from images");
$sth -> execute();

$SALIDA = $sth -> fetchrow_array;

### recontructing the image
##
$IMA = "/var/www/html/images/recovered_image.gif";
open A, ">$IMA";
print A $SALIDA;
close A;

$dbh -> disconnect();
1;

Finally, open your browse and point to:

http://yourserver/images/recoverd_image.gif

With a litte patient, you can with easy transport this scripts into
the language you use and of course, apply them in more complex
situations.

I hoppe you find this helpfull.

hachiman

Desmo
12-18-03, 08:30
Originally posted by eperich
ok
this picture data
is this a string or is this a real file

I mean did you made this string from a picture or do you have the string.

when you only have this string you must save this in text. and you MUST escape some characters.

Igf you have a file you only save the id from the BLOB in the table

I must look it uo. don't know it by heart.



Hi all,
but if I use bytea type, when I execute the insert query, the response is the same, "Warning: pg_exec(): Query failed: ERROR: parser: parse error at end of input . "

This is my code.
**********

//Open file in binary mode
$FileBinario = fopen("$upload_dir/$file_name", "rb");
//Read file
$DimensioneFile = filesize("$upload_dir/$file_name");

$contenuto = fread($FileBinario, $DimensioneFile );
//Close file
fclose($FileBinario);

//Insert in the table
$conn = pg_pconnect("dbname=test user=postgres");
pg_Exec ($conn, "INSERT INTO input_utente_dettaglio VALUES ($id_valore, $contenuto)");
pg_close($conn);

************

I'm working whit server web apache on linux whit php and postgreSQL

Regards!!!

eperich
12-18-03, 08:46
solution for this problem (http://at2.php.net/manual/de/function.pg-lo-open.php)

LOOK AT THE EXAMPLE BELOW

Desmo
12-18-03, 12:03
Originally posted by eperich
solution for this problem (http://at2.php.net/manual/de/function.pg-lo-open.php)

LOOK AT THE EXAMPLE BELOW



Thank yuo!!!!!! It's fantastic. Now I see the end of my project for University!!
Great eperich.

Desmo
12-28-03, 13:36
:-(((((((((((

Ok.... when I call the pg_lo_export function I recived this error....


/bann-pace.gif
Warning: pg_lo_export(): Requires 2 or 3 arguments. in /progetto/apache/htdocs/spez/generafile.php on line 25


I don't understand why......
This is my code...
<?
//connection to db
$conn = pg_pconnect("dbname=test user=postgres");
//execute the query for extract data
$result = pg_exec($conn, "SELECT info_utente_image, nome_file FROM input_utente_dettaglio, input_utente WHERE input_utente_dettaglio.id_dettaglio = input_utente.id_dettaglio
AND input_utente.id_user = 21 AND input_utente_dettaglio.id_tipo = 2;");
//create the string of the file that recives from db
$FileImmagine = $directory.'/'.$row[1];
//begin trunsaction
pg_query($conn, "begin");
//insert in a variable the data
$loid = pg_lo_open($conn, $row[0], "r");
// *** execute (but it fails) the function for restore the data from db in a file
pg_lo_export($conn, $loid, "/progetto/file.jpg");
pg_lo_close($loid);
pg_query($conn, "commit");
pg_close();


Can you help me?????

Desmo
12-29-03, 06:45
Hi,

Desmo
12-29-03, 06:55
...sorry, :-)

..a my friend give me the solution using another function (pg_lo_read) and open a file and write the data in the file. This is his code. It' so simple but I don't understand why the pg_lo_expoert function work correctly.... mmmm?????


<?
echo '<BR>';
$row = pg_fetch_row($result, $i);
$FileImmagine = $directory.'/'.$row[1];
echo $FileImmagine;
pg_query($conn, "begin");
$loid = pg_lo_open($conn, $row[0], "r");
$pippo=pg_lo_read($loid);
$file=fopen("$FileImmagine","w");
fwrite($file,$pippo);
fclose($file);
pg_lo_close($loid);
pg_query($conn, "commit");
?>

Bye

Margodth
01-19-04, 11:52
In my opinion, I do not suggest to store pictures into database. I think that it's better to store files on folders and make a table with a field pointing to where the picture it's stored.
i.e: /usr/myapp/pics
First reason is not to increase database files so much, so logs are going to been shorter and backups are going to be smaller.
Transactions are going to run faster and the sql engine will run smooth in comparison with databases storing pictures. (think in DBs with millions of records). Migrating to other environments are complicated. Storing files (binary data) means that you need a proper environment to do binary transactions. It is not recommended to store binary data, passing this data through a string variable. This may cause many problems. There are many reasons why I should kept them out of DB. But it also depends on the project, DB size and a lot more of things. But the most important thing... it's that I don't see the need of putting them into fields.

So this may turn the subjet to "wich is the better way for storing
a big amount of pictures files?". Well, that was not my meaning, I've only wanted to share my own experience.