0
我正嘗試使用psycopg2將大型二進制數據插入到postgresql中。我明白bytea數據類型更常用於使用,但對任何未來的使用情況測試BLOB。如何在psycopg2中使用lobject函數
postgresql和psycopg2的版本如下。
pip list | grep psycopg2
psycopg2 (2.5.1)
rpm -qa | grep postgres
postgresql-server-9.2.15-1.el7_2.x86_64
我使用Python 2.7.5
python -V
Python 2.7.5
下面是我的代碼片段
file = "/home/test/jefferson_love_memorial_514993.jpg"
with open(file,"r") as fd:
try:
# First connect to postgresql server
conn = psycopg2.connect("dbname='sample' user='sample' host='10.1.0.19' password='sample'")
# Initate the session with postgresql to write large object instance
lobj = conn.lobject(0,'r',0)
# Write the data to database
lobj.write(fd.read())
except (psycopg2.Warning, psycopg2.Error) as e:
print "Exception: {}".format(e)
然而,當我執行的代碼,我沒有錯誤,但沒有被插入到表。
-bash-4.2$ psql -d sample
psql (9.2.15)
Type "help" for help.
sample=# SELECT * FROM pg_largeobject_metadata;
lomowner | lomacl
----------+--------
(0 rows)
sample=# SELECT * FROM pg_largeobject;
loid | pageno | data
------+--------+------
(0 rows)
請問我的代碼中缺少什麼?