PostgreSQL,psycopg2和无OID表 [英] PostgreSQL, psycopg2 and OID-less tables

查看:91
本文介绍了PostgreSQL,psycopg2和无OID表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在已经在PostgreSQL中弃用了OID,你怎么找到新插入的记录的关键?


我试过三个Python客户端库,包括psycopg2,以及它们支持游标属性''lastrowid''(Python DB API 2.0),它总是

零。


有人想出来了吗?


谢谢。

-

Dale Strickland-Clark

Riverhall Systems - www.riverhall.co.uk

解决方案



Dale Strickland-Clark写道:


现在已经在PostgreSQL中弃用了OID,你怎么找到新插入的记录的关键字?


我试过三个Python客户端库,包括psycopg2,以及它们在哪里
支持游标属性''lastrowid''(Python DB A PI 2.0),总是

零。


有人想出来了吗?






这当然严格来说是一个PostgreSQL问题,但你试过吗


select lastval();





根据我对postgress手册的阅读,那应该做

把戏。


干杯,


--Tim


谢谢。

-

Dale Strickland-Clark

Riverhall Systems - www.riverhall.co.uk


Dale,


现在已经在PostgreSQL中弃用了OID,你如何找到新插入的记录键?

a?



使用OID作为主键对于某些PostgreSQL版本来说并不是一个好主意

allready ...即他们真的使dump&恢复更多

挑战。


所以通常我们都有:


CREATE TABLE feuser



id_user serial NOT NULL,

名称文字,

CONSTRAINT feuser_pkey PRIMARY KEY(id_user),

CONSTRAINT feuser_name_key UNIQUE(姓名)



没有OIDS;


自动为你创建一个序列重写序列号

" id_user"默认值为


nextval(''feuser_id_user_seq'':: regclass)


因此,要获取插入记录的密钥,基本上有两种

方法。 (从现在开始,cs是符合DB-API 2.0标准的游标对象,

即来自psycopg2


cs = cn.cursor()


a)先获取id,然后插入

cs.execute(" select nextval(''feuser_id_user_seq'':: regclass)")

newid = cs.fetchone()[0]


cs.execute(" ;插入feuser(id_user,name)值(%(id_user)s,

%(name)s)",

dict(id_user = newid,name = Karl Napf)


cs.commit()

-now newid包含你的新id。


b)在PostgreSQL上创建一个服务器端函数:

创建或替换函数insfeuser(文本)

RETURNS整数AS


BODY

Now that OIDs have been deprecated in PostgreSQL, how do you find the key of
a newly inserted record?

I''ve tried three Python client libraries, including psycopg2, and where they
support cursor attribute ''lastrowid'' (Python DB API 2.0), it is always
zero.

Anyone figured this out?

Thanks.
--
Dale Strickland-Clark
Riverhall Systems - www.riverhall.co.uk

解决方案


Dale Strickland-Clark wrote:

Now that OIDs have been deprecated in PostgreSQL, how do you find the key of
a newly inserted record?

I''ve tried three Python client libraries, including psycopg2, and where they
support cursor attribute ''lastrowid'' (Python DB API 2.0), it is always
zero.

Anyone figured this out?

Hi,

It''s of course strictly a PostgreSQL question, but have you tried

select lastval();

?

According to my reading of the postgress manuals, that should do the
trick.

Cheers,

--Tim

Thanks.
--
Dale Strickland-Clark
Riverhall Systems - www.riverhall.co.uk


Dale,

Now that OIDs have been deprecated in PostgreSQL, how do you find the keyof
a newly inserted record?

using OIDs as primary key was no good idea for some PostgreSQL versions
allready ... i.e. they really make dump & restore much more
challenging.

So usually us have something along:

CREATE TABLE feuser
(
id_user serial NOT NULL,
name text,
CONSTRAINT feuser_pkey PRIMARY KEY (id_user),
CONSTRAINT feuser_name_key UNIQUE (name)
)
WITHOUT OIDS;

which automatically creates a sequence for you and rewrites the serial
"id_user" with a default of

nextval(''feuser_id_user_seq''::regclass)

So, to get the key of a record inserted, basically there are two
methods. (from now on "cs" is a DB-API 2.0 compliant cursor object,
i.e. from psycopg2

cn=psycopg2.connect(....)
cs=cn.cursor()

a) get id first, then insert
cs.execute("select nextval(''feuser_id_user_seq''::regclass)")
newid=cs.fetchone()[0]

cs.execute("insert into feuser (id_user, name) values (%(id_user)s,
%(name)s)",
dict(id_user=newid, name="Karl Napf")

cs.commit()
-now newid contains your new id.

b) create a serverside function on PostgreSQL:
CREATE OR REPLACE FUNCTION insfeuser (text)
RETURNS integer AS


BODY


这篇关于PostgreSQL,psycopg2和无OID表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆