= psycopg.connect(user="postgres", password="example", host="localhost", port=5432, dbname="postgres")
with conn.cursor() as cursor, conn.cursor() as cursor2:
# both cursor and cursor2 will use the same connection,
# so any way queries are sequential
cursor.execute("insert into test1 (id) values (uuid_generate_v4());")
cursor2.execute("insert into test1 (id) values (uuid_generate_v4());")
cursor2.rollback() # this will fail because Cursor does not have rollback method
conn.commit() # this will commit any changes made by cursor and cursor2
conn.close()
so what is the perpose of Cursor (not ServerCursor) in psycopg as it gives allmost nothing special conpared to connection?
compared to asyncpg which makes all the same without extra abstraction
They are used to read the data
yes. but question is more about why not they use connection without this confusing abstraction imho: if we compare Cursor behavior to real ServerCursor it's really confusing
The cursor have the records positioning If you'd remove the cursor you couldn't read partially the data and every selection would void the previous selection
in case or Cursor data is already transferd to client from db server. you are free to do anything you like with it only ServerCursor gives power of postgres cursor with chunk reads
This isn't relevant Cursors have positioning, the above's still valid
cursor has positioning but in case of usual (not server) cursor they could just give it to user without any coursor data is already in memory user could iterate it any way he likes to be more clear this code will work without any problems def main(): conn = psycopg.connect(user="postgres", password="example", host="localhost", port=5432, dbname="postgres") with conn.cursor() as cursor: cursor.execute("insert into test1 (id) values (uuid_generate_v4()) returning id;") cursor.connection.commit() time.sleep(30) # stop database container print(cursor.fetchall()) # -> [(UUID('98b26338-61ba-448a-8953-d1bc49e86f93'),)]
This code don't use positioning
same def main(): conn = psycopg.connect(user="postgres", password="example", host="localhost", port=5432, dbname="postgres") with conn.cursor() as cursor: cursor.execute("select * from test1;") time.sleep(20) # stop database container cursor.scroll(2) print(cursor.fetchmany(2)) # -> [(UUID('3eaeccf6-aa60-493d-9f1a-219a600edba6'),), (UUID('98b26338-61ba-448a-8953-d1bc49e86f93'),)]
Ok now do two queries Read a record from the first query and compare the result from the 2nd query, one record per time
and what should it show to me? what I'm talking about is that Cursor abstraction is useless here just return to the user (one, N, all) as everything already in memory query by query and let user process data in any way just compare psycopg and asyncpg approaches and you'll understand what I'm takking about
why cursor.scroll() ?
I doubt you read the answers Your code has no positioning and it will not be possible without cursors Also I explained the Google example Also that's part of dbapi2
seems like dbapi2 spec is the only argument here relevant for me cause everything else works fine without it thanks
Обсуждают сегодня