Обсуждение: reading reading files from the DB

Поиск
Список
Период
Сортировка

reading reading files from the DB

От
"sandhya"
Дата:
 Hi.....

I have exported few files into my DB server running on LINUX os.
I connected to it remotely from windows and through one sample application
like webserver i am trying to read the contents of it when ever there is a
request for the specified file.Using the below example code.
 Is this correct?I am not finding that it is reading it from the database.
How can i make sure of this.I am unable to open the requested file from win
application through database.
 please suggest me .........
 voi d GetFiles(PGconn *conn, unsigned int lobjId, int start)
 {

 ---------

 ------------

 int lobj_fd;

 lobj_fd = lo_open(conn, lobjId, INV_READ);

 if (lobj_fd < 0)

 {

 MessageBox(0,"error","Cannot Object Id ",MB_OK);

 }

 lo_lseek(conn, lobj_fd, start, SEEK_END);

 len = lo_tell(conn,lobj_fd);

 lo_lseek(conn, lobj_fd, start, SEEK_SET);

 buf =(char*) malloc(len + 1);

 nread = 0;

 while (len - nread > 0)

 {

 nbytes = lo_read(conn, lobj_fd, buf, len - nread);

 buf[nbytes] = ' ';

 nread += nbytes;

 }

 MessageBox(0,"Inside get","test",MB_OK);

 free(buf);

 lo_close(conn, lobj_fd);

 }



 Thank you.....
 Sandhya

> ----- Original Message -----
> From: "Tom Lane" <tgl@sss.pgh.pa.us>
> To: "sandhya" <sandhyar@amiindia.co.in>
> Cc: "postgre" <pgsql-admin@postgresql.org>
> Sent: Monday, December 26, 2005 9:08 PM
> Subject: Re: [ADMIN] reg:lseek&read ..pls
>
>
> > "sandhya" <sandhyar@amiindia.co.in> writes:
> > > But before that i need to know the size of the object from which i am
=
> > > trying to get the contents.
> > > How can i do this?
> >
> > Same way you'd do it for a Unix file: seek to the end, note the end
> > offset, seek back to the start and read.
> >
> > ... lo_open ...
> > lo_size = lo_lseek(conn, fd, 0, SEEK_END);  // where's the end?
> > lo_lseek(conn, fd, 0, SEEK_SET);  // go back to start
> >
> > regards, tom lane
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 2: Don't 'kill -9' the postmaster
> >
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>




Re: reading reading files from the DB

От
Michael Fuhr
Дата:
On Wed, Dec 28, 2005 at 04:40:02PM +0530, sandhya wrote:
> I have exported few files into my DB server running on LINUX os.
> I connected to it remotely from windows and through one sample application
> like webserver i am trying to read the contents of it when ever there is a
> request for the specified file.Using the below example code.
>  Is this correct?I am not finding that it is reading it from the database.
> How can i make sure of this.I am unable to open the requested file from win
> application through database.

What are you expecting to happen and what actually does happen?

>  lobj_fd = lo_open(conn, lobjId, INV_READ);
>  if (lobj_fd < 0)
>  {
>  MessageBox(0,"error","Cannot Object Id ",MB_OK);
>  }

Does MessageBox() return control to the program?  If so then you
should return or exit in case of error because there's no point
continuing if lo_open() failed, and calling other lo_* functions
will almost certainly cause problems.  Also, you can use PQerrorMessage()
to get an error message explaining the failure.

>  lo_lseek(conn, lobj_fd, start, SEEK_END);
>  len = lo_tell(conn,lobj_fd);
>  lo_lseek(conn, lobj_fd, start, SEEK_SET);
>  buf =(char*) malloc(len + 1);

A robust program would check these calls for errors.

>  nread = 0;
>  while (len - nread > 0)
>  {
>  nbytes = lo_read(conn, lobj_fd, buf, len - nread);
>  buf[nbytes] = ' ';
>  nread += nbytes;
>  }

I'm not sure the loop is necessary, but if you keep the loop then
you're not handling buf correctly.  As written, each call to lo_read()
will store data at buf, overwriting what was written there before.
And why are you appending a space?  Did you mean to append a NUL
('\0') string-terminating character?  That would make sense only
if you know the large object data can be handled as a string, i.e.,
that it doesn't contain binary data where NUL might be present in
the middle of the data.  And again, a robust program would check
lo_read()'s return value before continuing.

--
Michael Fuhr

Re: reading reading files from the DB

От
Michael Fuhr
Дата:
On Wed, Dec 28, 2005 at 09:47:34PM -0700, Michael Fuhr wrote:
> What are you expecting to happen and what actually does happen?

Another thing: are you calling the lo_* functions within a transaction?
As the Client Interfaces section of the Large Objects chapter in
the documentation says, "All large object manipulation using these
functions must take place within an SQL transaction block."  If you
call lo_open() outside a transaction block then it will fail and
PQerrorMessage() will return a string like "invalid large-object
descriptor: 0".

--
Michael Fuhr