2004-10-19 07:17:12 +04:00
|
|
|
# binary.py - working with binary data
|
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# Copyright (C) 2001-2010 Federico Di Gregorio <fog@debian.org>
|
2004-10-19 07:17:12 +04:00
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2004-10-19 07:17:12 +04:00
|
|
|
#
|
2010-02-13 01:34:53 +03:00
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
## put in DSN your DSN string
|
|
|
|
|
|
|
|
DSN = 'dbname=test'
|
|
|
|
|
2011-12-16 03:58:22 +04:00
|
|
|
## don't modify anything below this line (except for experimenting)
|
2017-12-04 05:47:19 +03:00
|
|
|
from __future__ import print_function
|
2004-10-19 07:17:12 +04:00
|
|
|
|
2005-03-01 19:41:02 +03:00
|
|
|
import sys
|
2005-06-13 10:25:06 +04:00
|
|
|
import psycopg2
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
DSN = sys.argv[1]
|
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Opening connection using dsn:", DSN)
|
2005-06-13 10:25:06 +04:00
|
|
|
conn = psycopg2.connect(DSN)
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Encoding for this connection is", conn.encoding)
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
curs = conn.cursor()
|
|
|
|
try:
|
|
|
|
curs.execute("CREATE TABLE test_binary (id int4, name text, img bytea)")
|
|
|
|
except:
|
|
|
|
conn.rollback()
|
|
|
|
curs.execute("DROP TABLE test_binary")
|
|
|
|
curs.execute("CREATE TABLE test_binary (id int4, name text, img bytea)")
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
# first we try two inserts, one with an explicit Binary call and the other
|
|
|
|
# using a buffer on a file object.
|
|
|
|
|
|
|
|
data1 = {'id':1, 'name':'somehackers.jpg',
|
2005-06-13 10:25:06 +04:00
|
|
|
'img':psycopg2.Binary(open('somehackers.jpg').read())}
|
2004-10-19 07:17:12 +04:00
|
|
|
data2 = {'id':2, 'name':'whereareyou.jpg',
|
|
|
|
'img':buffer(open('whereareyou.jpg').read())}
|
|
|
|
|
|
|
|
curs.execute("""INSERT INTO test_binary
|
2005-03-02 17:51:24 +03:00
|
|
|
VALUES (%(id)s, %(name)s, %(img)s)""", data1)
|
2004-10-19 07:17:12 +04:00
|
|
|
curs.execute("""INSERT INTO test_binary
|
2005-03-02 17:51:24 +03:00
|
|
|
VALUES (%(id)s, %(name)s, %(img)s)""", data2)
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
# now we try to extract the images as simple text strings
|
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Extracting the images as strings...")
|
2004-10-19 07:17:12 +04:00
|
|
|
curs.execute("SELECT * FROM test_binary")
|
|
|
|
|
|
|
|
for row in curs.fetchall():
|
|
|
|
name, ext = row[1].split('.')
|
|
|
|
new_name = name + '_S.' + ext
|
2017-12-04 05:47:19 +03:00
|
|
|
print(" writing %s to %s ..." % (name+'.'+ext, new_name), end=' ')
|
2004-10-19 07:17:12 +04:00
|
|
|
open(new_name, 'wb').write(row[2])
|
2017-12-04 05:47:19 +03:00
|
|
|
print("done")
|
|
|
|
print(" python type of image data is", type(row[2]))
|
2017-12-02 08:37:49 +03:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
# extract exactly the same data but using a binary cursor
|
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
print("Extracting the images using a binary cursor:")
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
curs.execute("""DECLARE zot CURSOR FOR
|
|
|
|
SELECT img, name FROM test_binary FOR READ ONLY""")
|
|
|
|
curs.execute("""FETCH ALL FROM zot""")
|
|
|
|
|
|
|
|
for row in curs.fetchall():
|
|
|
|
name, ext = row[1].split('.')
|
|
|
|
new_name = name + '_B.' + ext
|
2017-12-04 05:47:19 +03:00
|
|
|
print(" writing %s to %s ..." % (name+'.'+ext, new_name), end=' ')
|
2004-10-19 07:17:12 +04:00
|
|
|
open(new_name, 'wb').write(row[0])
|
2017-12-04 05:47:19 +03:00
|
|
|
print("done")
|
|
|
|
print(" python type of image data is", type(row[0]))
|
2017-12-02 08:37:49 +03:00
|
|
|
|
2013-04-26 12:21:56 +04:00
|
|
|
# this rollback is required because we can't drop a table with a binary cursor
|
2004-10-19 07:17:12 +04:00
|
|
|
# declared and still open
|
|
|
|
conn.rollback()
|
|
|
|
|
|
|
|
curs.execute("DROP TABLE test_binary")
|
|
|
|
conn.commit()
|
|
|
|
|
2017-12-04 05:47:19 +03:00
|
|
|
print("\nNow try to load the new images, to check it worked!")
|