A
A
AlerX2019-05-19 23:41:52
Python
AlerX, 2019-05-19 23:41:52

Why does Postgresql throw an error on execute query?

There was a question, I have already faced the problem many times, sometimes it was solved after a couple of launches of the program (Which is rather strange). And so, the problem is that when I execute a database query, postgresql gives me an error.
The code:

money = sqlexecute(f"SELECT money FROM main WHERE user_id = {str(user_id)}")[0]

So sqlexecute is a function that returns the contents of a
Function:
def sqlexecute(execute):
    sql.execute(str(execute))
    return sql.fetchall()[0]

After that, I try to display the contents of the money variable,
which gives me an error
Error:
File "...", line 673, in moneycheck
    money = sqlexecute(f"SELECT money FROM main WHERE user_id = {str(user_id)}")[0]
  File "...", line 655, in sqlexecute
    return sql.fetchall()[0]
psycopg2.ProgrammingError: no results to fetch

sql is a cursor
sqz = psycopg2.connect(database='...', user='...', password='...')
sql = sqz.cursor()

Maybe I'm missing something, but I couldn't find a solution to this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kova1ev, 2019-05-20
@kova1ev

fetchall() must be applied to the result of the response, and not to the pointer to the base

def sqlexecute(query):
    return sql.execute(query).fetchall()[0]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question