1

Craig Ringer yo no puedo trabajar con large object functions

My database looks like this

this is my table

-- Table: files
    -- 
DROP TABLE files;

CREATE TABLE files

(   
id serial NOT NULL,

orig_filename text NOT NULL,

file_data bytea NOT NULL,

CONSTRAINT files_pkey PRIMARY KEY (id)

)

WITH (

OIDS=FALSE

);

ALTER TABLE files

I want save .pdf in my database, I saw you did the last answer, but using python27 (read the file and convert to a buffer object or use the large object functions)

I did the code would look like

path="D:/me/A/Res.pdf"
listaderuta = path.split("/")
longitud=len(listaderuta)
f = open(path,'rb')
f.read().__str__()
cursor = con.cursor()
cursor.execute("INSERT INTO files(id, orig_filename, file_data) VALUES (DEFAULT,%s,%s) RETURNING id", (listaderuta[longitud-1], f.read()))

but when I'm downloading, ie save

fula = open("D:/INSTALL/pepe.pdf",'wb')
cursor.execute("SELECT file_data, orig_filename FROM files WHERE id = %s", (int(17),))
(file_data, orig_filename) = cursor.fetchone()
fula.write(file_data)
fula.close()

but when I'm downloading the file can not be opened, this damaged I repeat I can not work with large object functions

try this and turned me, can you help ?

user3328955
  • 95
  • 14
  • "see the psycopg2 and postgresql documentation." Did you take a look at psycopg2's documentation? What's your database schema like - are you using large objects, bytea fields, or something else? – Craig Ringer Feb 19 '14 at 16:25
  • See the "edit" link? Please use that to add the new info *with proper indenting using the {} button* then comment here when done. Much easier to read. – Craig Ringer Feb 19 '14 at 16:58
  • sorry but the amount of characters does not allow me to write all – user3328955 Feb 19 '14 at 17:05
  • Did you *look at the large object support in psycopg2*? Go read the manual please: http://initd.org/psycopg/docs/usage.html#access-to-postgresql-large-objects, http://www.postgresql.org/docs/current/static/largeobjects.html . If you're still stuck after making an effort to solve this yourself, please **edit your question** with the added info you've shoved into comments, explain what you tried and why it didn't work, *then* comment here. – Craig Ringer Feb 19 '14 at 17:05
  • You can write more by **editing your question with the edit link below the question text**. I think you need to visit http://stackoverflow.com/tour and http://stackoverflow.com/help/asking . – Craig Ringer Feb 19 '14 at 17:07
  • Edit the question again, I just want to see how you do to read the file and convert to a buffer object and to do the reverse, you understand me? – user3328955 Feb 19 '14 at 17:53
  • I need is the same example you did in [link] (http://stackoverflow.com/questions/16763904/how-to-save-a-image-file-on-a-postgres-database) but in python 2.7 Hopefully you can help – user3328955 Feb 19 '14 at 17:59
  • Read http://initd.org/psycopg/docs/usage.html#adapt-binary for guidance on how to use `bytea` with Python 2.7. – Craig Ringer Feb 20 '14 at 12:27

1 Answers1

2

I updated my previous answer to indicate usage for Python 2.7. The general idea is to read the manual and follow the instructions there.

Here's the relevant part:

In Python 2, you can't just pass a str directly, as psycopg2 will think it's an encoded text string, not raw bytes. You must either use psycopg2.Binary to wrap it, or load the data into a bytearray object.

So either:

 filedata = psycopg2.Binary( f.read() )

or

 filedata = buffer( f.read() )
Community
  • 1
  • 1
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • Craig Ringer thank you very much for your reply! I am eternally grateful!! the end had already managed to do thanks to his advice, data = f.read () buf = bytearray (data) filedata = memoryview (buf) I am now trying to find out if a certificate is revoked or not, by the CRL, but hey that will be another adventure that maybe you can not help me. 1000 thanks again – user3328955 Feb 20 '14 at 20:46