Google App Engine: Datastore Query issue

This request works:

item = db.GqlQuery("SELECT * FROM Item WHERE CSIN = 13")[0]

although if the results are not returned, it will explode in my face. (How can I get around this? The loop forseems dubious when I want at maximum iteration.)

This request does not work:

item = db.GqlQuery("SELECT * FROM Item WHERE CSIN = :1", CSIN)[0]

CSINis a string representing a number. I get this error:

Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 507, in __call__
    handler.get(*groups)
  File "path\to\src\Main.py", line 42, in get
    item = db.GqlQuery("SELECT * FROM Item WHERE CSIN = :1", CSIN)[0]
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 1717, in __getitem__
    raise IndexError('The query returned fewer than %d results' % (arg+1))
IndexError: The query returned fewer than 1 results

What am I doing wrong here?

+3
source share
2 answers

You are trying to get an item from a list (or list) that is empty. What you do is like the following:

>>> results = [] # an empty list
>>> item = results[0] # Raises an IndexError, because there is nothing in the list

Instead, you need to:

item = db.GqlQuery("SELECT * FROM Item WHERE CSIN = :1", CSIN).get()

Then it itemwill be either the Nonefirst result of the query.

+9
source

, - , . .

.

0

Source: https://habr.com/ru/post/1735076/


All Articles