Python/postgres/psycopg2:获取刚刚插入的行的ID
我正在使用 Python 和 psycopg2 来连接 postgres。
当我插入一行时...
sql_string = "INSERT INTO hundred (name,name_slug,status) VALUES ("
sql_string += hundred_name + ", '" + hundred_slug + "', " + status + ");"
cursor.execute(sql_string)
...如何获取刚刚插入的行的 ID?尝试:
hundred = cursor.fetchall()
返回错误,而使用RETURNING id
:
sql_string = "INSERT INTO domes_hundred (name,name_slug,status) VALUES ("
sql_string += hundred_name + ", '" + hundred_slug + "', " + status + ") RETURNING id;"
hundred = cursor.execute(sql_string)
仅返回None
。
更新: currval
也是如此(即使直接在 postgres 中使用此命令也可以):
sql_string = "SELECT currval(pg_get_serial_sequence('hundred', 'id'));"
hundred_id = cursor.execute(sql_string)
任何人都可以建议吗?
谢谢!
I'm using Python and psycopg2 to interface to postgres.
When I insert a row...
sql_string = "INSERT INTO hundred (name,name_slug,status) VALUES ("
sql_string += hundred_name + ", '" + hundred_slug + "', " + status + ");"
cursor.execute(sql_string)
... how do I get the ID of the row I've just inserted? Trying:
hundred = cursor.fetchall()
returns an error, while using RETURNING id
:
sql_string = "INSERT INTO domes_hundred (name,name_slug,status) VALUES ("
sql_string += hundred_name + ", '" + hundred_slug + "', " + status + ") RETURNING id;"
hundred = cursor.execute(sql_string)
simply returns None
.
UPDATE: So does currval
(even though using this command directly into postgres works):
sql_string = "SELECT currval(pg_get_serial_sequence('hundred', 'id'));"
hundred_id = cursor.execute(sql_string)
Can anyone advise?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
并且请不要手动构建包含值的 SQL 字符串。您可以(并且应该!)单独传递值,从而无需转义并且无法进行 SQL 注入:
请参阅 psycopg 文档以获取更多详细信息:http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries
And please do not build SQL strings containing values manually. You can (and should!) pass values separately, making it unnecessary to escape and SQL injection impossible:
See the psycopg docs for more details: http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries
我最终来到这里是因为我遇到了类似的问题,但我们使用的是 Postgres-XC,它尚不支持 RETURNING ID 子句。在这种情况下,您可以使用:
以防万一它对任何人都有用!
I ended up here because I had a similar problem, but we're using Postgres-XC, which doesn't yet support the RETURNING ID clause. In that case you can use:
Just in case it was useful for anyone!
考虑 RETURNING 子句 http://www.postgresql.org/docs/8.3 /static/sql-insert.html
Consider a RETURNING clause http://www.postgresql.org/docs/8.3/static/sql-insert.html
对我来说,盗贼大师的答案和杰米·布朗的答案都不起作用。对我有用的是两者的结合,我想在这里回答,以便它可以帮助其他人。
我需要做的是:
语句
lastid = curve.fetchone()['lastval']
对我不起作用,即使在cursor.execute('SELECT LASTVAL()' 之后)
。单独的语句id_of_new_row = curve.fetchone()[0]
也不起作用。也许我错过了一些东西。
For me, neither ThiefMaster's answer worked nor Jamie Brown's. What worked for me was a mix of both, and I'd like to answer here so it can help someone else.
What I needed to do was:
The statement
lastid = cursor.fetchone()['lastval']
didn't work for me, even aftercursor.execute('SELECT LASTVAL()')
. The statementid_of_new_row = cursor.fetchone()[0]
alone didn't work either.Maybe I'm missing something.
ThiefMaster 的方法对我来说很有效,适用于
INSERT
和UPDATE
命令。如果在执行
INSERT
/UPDATE
命令后在游标上调用cursor.fetchone()
但缺少返回值 (RETURNING
子句)将引发异常:ProgrammingError('no results to fetch'))
ThiefMaster's approach worked for me, for both
INSERT
andUPDATE
commands.If
cursor.fetchone()
is called on a cursor after having executed anINSERT
/UPDATE
command but lacked a return value (RETURNING
clause) an exception will be raised:ProgrammingError('no results to fetch'))