A
A
AlmazKayum2018-03-23 00:57:35
Python
AlmazKayum, 2018-03-23 00:57:35

Why don't SELECT queries work in Psycopg2?

Library psycopg2-2.7.4

import psycopg2
class Database:
    def __init__(self, database):
        self.conn = psycopg2.connect(database)
        self.cursor = self.conn.cursor()
    def put_balance(self, bal, chat_id):
        with self.conn:
            self.cursor.execute('UPDATE profile SET points = %s WHERE chat_id = %s', (bal, chat_id))
    def get_balance(self, chat_id):
        with self.conn:
            return self.cursor.execute('SELECT points FROM profile WHERE chat_id = %s;', (chat_id,))

In the Database class, all SELECT methods return None, while all UPDATE and INSERT methods work.
When you use the fetchall() or fetchone() method, the following error occurs:
return self.cursor.execute('SELECT points FROM profile WHERE chat_id = %s;', (chat_id,)).fetchall()
AttributeError: 'NoneType' object has no attribute 'fetchall'
What could be the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2018-03-23
@AlmazKayum

initd.org/psycopg/docs/usage.html

# Query the database and obtain data as Python objects
>>> cur.execute("SELECT * FROM test;")
>>> cur.fetchone()
(1, 100, "abc'def")

# Make the changes to the database persistent
>>> conn.commit()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question