Похожие чаты

Random thoughts. Need mode opinions. def main(): conn

= 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

13 ответов

41 просмотр

They are used to read the data

Artem- Автор вопроса
Muflone
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

Artem
yes. but question is more about why not they use c...

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

Artem- Автор вопроса
Muflone
The cursor have the records positioning If you'd ...

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

Artem
in case or Cursor data is already transferd to cli...

This isn't relevant Cursors have positioning, the above's still valid

Artem- Автор вопроса
Muflone
This isn't relevant Cursors have positioning, the...

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'),)]

Artem- Автор вопроса
Muflone
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'),)]

Artem
same def main(): conn = psycopg.connect(user=...

Ok now do two queries Read a record from the first query and compare the result from the 2nd query, one record per time

Artem- Автор вопроса
Muflone
Ok now do two queries Read a record from the firs...

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

Artem
and what should it show to me? what I'm talking ab...

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

Artem- Автор вопроса
Muflone
I doubt you read the answers Your code has no pos...

seems like dbapi2 spec is the only argument here relevant for me cause everything else works fine without it thanks

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта