Похожие чаты

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 ответов

16 просмотров

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

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

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

In a country where censorship and arbitrary decisions are the order of the day, offering a message of hope to workers and youth is necessary, but it is only possible with expe...
Ibrahim Rabiu
1
How did we have so many dormant scammers in here? Who are these people. It’s been years. We know each other. If you dm me “I’ve been an og” but haven’t said a single word her...
Justin | LGCY |
42
Hello Everyone 🙌🏻💜 Happy (?) Friday to everyone 😁😁 I have read through all the comments/feedback/complaints in the last 2 days following the CEO Update AMA. Clearly the ...
Patrick - Ultra.io
4
Kraken has just announced they won t support the merger of Fet and Ocean (Agix not available anyways)! This means we on Kraken are forced to close our position on loss or tran...
MG
21
Absolutely! 100+ countries? That's insane. 🌍🔥
Josh
26
Кстати, а я вот тут подумал. Допустим, у нас имеется цикл который выполняет огромное количество итераций, но мы хотим, чтобы какие-то действия исполнилось только один раз. В Я...
The Bird of Hermes
23
Доброй ночи. Вопрос знатокам. Имеется некая таблица, результат которой выведен в DBGrid на форме. И есть форма, с помощью которой можно как добавить запись, так и отредактиров...
Евгений
28
а всё почему? потому что ассемблер в отличии от яву порождает множество пагубных привычек, среди которых например можно отметить использование глобальных переменных для всего ...
Mixail Frolov
35
🌟 Great News from Dypius! 🌟 We're happy to announce the Early Access launch of the Dypius Mobile App by the end of this month! Let's dive into the features you'll get to exp...
DYP | Tim
3
Guys I want to exec into a container running on Fargate like this: aws ecs execute-command --cluster my-cluster --task task_id --container app --command bash --interactive ...
Ehsan
14
Карта сайта