Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python appengine Query does not work when using a variable

I am trying to use a fetcher method to retrieve items from my datastore. If I use the following

def getItem(item_id):
    q = Item.all()
    q.filter("itemid = ", item_id)

It fails because nothing is returned. If I hard code in an item like

def getItem(item_id):
    q = Item.all()
    q.filter("itemid = ", 9000)

it fetches just fine, and sings merrily along. I have tried every which way to get this to work. I have used

result = db.GqlQuery("SELECT * FROM Item WHERE item_id = :1 LIMIT 1",
    title).fetch(1)

to the same effect. If I hard code in a number, works fine. I have tried setting the select statement as a local string, assembling it that way, casting the int as a string, and nothing. When I output the SELECT statement to the screen, looks fine. I can cut ans paste the output into the string, and whammo, it works. Any help would be appreciated.

like image 588
Lloyd Avatar asked Apr 18 '26 05:04

Lloyd


1 Answers

Does it make any difference if you do this:

def getItem(item_id):
    q = Item.all()
    q.filter("itemid = ", int(item_id))

The most likely cause of the problem that I can see is that the item_id parameter may be a string even though it is holding a numerical value. Coerce it to an int, and see if that makes any difference.

like image 109
Adam Crossland Avatar answered Apr 21 '26 01:04

Adam Crossland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!