我有一张桌子:
CREATE TABLE public."Persons"
(
id_persons serial NOT NULL,
name_persons varchar(50) NOT NULL,
telephone_persons varchar(14) NOT NULL,
email_persons varchar(100),
address_persons integer NOT NULL,
photo_persons oid,
login_persons varchar(20) NOT NULL,
password_persons varchar(32) NOT NULL,
type_persons public."PersonTypes" NOT NULL DEFAULT 'Client',
CONSTRAINT "Persons_pkey" PRIMARY KEY (id_persons),
CONSTRAINT "Persons-email_ukey" UNIQUE (email_persons),
CONSTRAINT "Persons-login_ukey" UNIQUE (login_persons)
);
在我的本地电脑上,我有photo.gif
个文件,我需要将此文件发送到photo_persons
表格中的Person
列。
我想通过存储过程并使用cursor.callproc()
psycopg2方法执行此操作。
我需要一些存储过程和psycopg2示例来获得这个。
答案 0 :(得分:1)
您需要传递的oid
是大对象本身的oid
,而不是函数。
假设某个连接对象conn
:
conn = psycopg2.connect(...)
可以通过几种方式创建和填充大型对象,例如:
# Open for writing in binary mode. No oid is passed, so a new object
# will be created.
new_lob = conn.lobject(mode="wb")
# Stash the newly allocated oid.
new_oid = new_lob.oid
# Put some data in the object.
new_lob.write(b"some data")
# And close it out.
new_lob.close()
或者,要从本地文件到运行代码的位置创建和填充大对象,您可以执行以下操作:
# Open for writing in binary mode. No oid is passed, so a new object
# will be created. psycopg2 will take care of loading the contents
# of the file named "somefile.ext".
new_lob = conn.lobject(mode="wb", new_file="somefile.ext")
# Stash the newly allocated oid.
new_oid = new_lob.oid
# And close it out.
new_lob.close()
要从先前创建的大型对象加载数据,请将oid
传递给lobject
,例如:
# Open the object identified by new_oid in binary reading mode.
loaded_lob = conn.lobject(oid=new_oid, mode="rb")
# Load all of its data.
loaded_lob.read() == b"some data"
如果您使用第二种方法来创建对象,即通过传入文件名,您可以将生成的oid
传递给您的函数,然后将该标识符存储在您的表中。请注意,您可能希望在同一事务中执行这两个步骤 - 创建大对象和创建表条目。