Selecting Datastore ID in Google App Engine? - python

I'm trying to make a query that selects everything where the id is 6. The problem is that I cant seem to get it to work. This is what the code looks like at the moment:
query = db.GqlQuery("SELECT * FROM Users WHERE id = 6")
result = query.get()
for result in query:
self.response.out.write(result.username)
Theres no errors or anything but it just wont output the username. Has anyone had this problem before or know what I did wrong?

If you're using the id value assigned by the datastore, there can only be a single entity with a given id.
How about this instead:
idNum = 6
# handy function the datastore API provides...
user = Users.get_by_id(idNum)
self.response.out.write(user.username)

Related

SQL LIKE using variable from user as a search

I'm trying to make a search functionality to output all available books based on author typed in a form (I'm using sqlite)
I'd like to use LIKE function.
The piece of code I'm using is as follows:
# User reached route via POST (submitting the form)
else:
query = request.form.get("query")
# Ensure symbol is not blank
if not query:
return apology("you haven't typed anything!", 400)
# A list querying available books by a similar author
books = db.execute("SELECT author, title, genre, id FROM books WHERE booked = 0 AND author LIKE '%' + :query + '%'",
query = query)
However, it doesn't seem to work.
No errors are found, but when I try to print out books, it returns always empty, even if I type in the exact author name.
How can I fix this?
Thanks!

Python/SQL Server - Subquery inside of a SELECT problem

I try to make my queries a little bit better and also try to reduce the amont of them I have inside my code.
So I stumbled over subqueries. The thing I want to achieve now is that I SELECT the BankID in my Entity Table and with that I directly want to get the Name of the BankID inside of my Bank Table.
I use Python and SQL Server with a Pooled DB. If i missed something please let me know!
At the moment my code is just getting the ID:
import DB_Pool
ms = DB_Pool.Database()
entity_id = 1
entity_data = ms.ExecQuery("SELECT Name,BankID FROM Entity WHERE EntityID = ? AND IsCurrent = 1",(entity_id,))
print(entity_data)
This is working fine.
But I cant get my head around on where to add the BankName SELECT with the BankID I got from the Entity Table?
You seem to be looking for a join. Assuming that Entity and Bank relate through column BankID, that would be:
SELECT e.Name entityName, e.BankID, b.Name bankName
FROM Entity e
INNER JOIN Bank b on b.BankID = e.BankID
WHERE e.EntityID = ? AND e.IsCurrent = 1

How to Query from salesforce the OpportunityFieldHistory with python

I am having an issue figuring out how to start a query on the OpportunityFieldHistory from Salesforce.
The code I usually use and works for querying Opportunty or Leads work fine, but I do not know how should be written for the FieldHistory.
When I want to query the opportunity or Lead I use the following:
oppty1 = sf.opportunity.get('00658000002vFo3')
lead1 = sf.lead.get('00658000002vFo3')
and then do the proper query code with the access codes...
The problem arises when I want to do the analysis on the OpportunityFieldHistory, I tried the following:
opptyhist = sf.opportunityfieldhistory.get('xxx')
Guess what, does not work. Do you have any clue on what should I write between sf. and .get?
Thanks in advance
Looking at the simple-salesforce API, it appears that the get method accepts an ID which you are passing correctly. However, a quick search in the Salesforce API reference seems to indicate that the OpportunityFieldHistory may need to be obtained by another function such as get_by_custom_id(self, custom_id_field, custom_id).
(OpportunityFieldHistory){
Id = None
CreatedDate = 2012-08-27 12:00:03
Field = "StageName"
NewValue = "3.0 - Presentation & Demo"
OldValue = "2.0 - Qualification & Discovery"
OpportunityId = "0067000000RFCDkAAP"
},

Google App Engine - ndb sorting by string? (Python)

Is it possible to sort query results by StringProperty?
I have the following:
class User(ndb.Model):
first_name = ndb.StringProperty(indexed=False)
last_name = ndb.StringProperty(indexed=False)
Now if I want to retrieve the stored entries from the database, I use this (it works):
user_query = User.query(ancestor=user_key(user_name))
But I want the result to be ordered by first_name, so I use this:
user_query = User.query(ancestor=user_key(user_name)).order(-User.first_name)
This DOES NOT work! I don't know what is wrong and it does not produce any errors, but no results show up anymore. All I get is an empty table :-(
You need an index for order to work.

Efficient way to do large IN query in Google App Engine?

A user accesses his contacts on his mobile device. I want to send back to the server all the phone numbers (say 250), and then query for any User entities that have matching phone numbers.
A user has a phone field which is indexed. So I do User.query(User.phone.IN(phone_list)), but I just looked at AppStats, and is this damn expensive. It cost me 250 reads for this one operation, and this is something I expect a user to do often.
What are some alternatives? I suppose I can set the User entity's id value to be his phone number (i.e when creating a user I'd do user = User(id = phone_number)), and then get directly by keys via ndb.get_multi(phones), but I also want to perform this same query with emails too.
Any ideas?
You could create a PhoneUser model like so:
from google.appengine.ext import ndb
class PhoneUser(ndb.Model):
number = ndb.StringProperty()
user = ndb.KeyProperty()
class User(ndb.Model):
pass
u = User()
u.put()
p = PhoneUser(id='123-456-7890', number='123-456-7890', user=u.key)
p.put()
u2 = User()
u2.put()
p2 = PhoneUser(id='555-555-5555', number='555-555-5555', user=u2.key)
result = ndb.get_multi([ndb.Key(PhoneUser, '123-456-7890'), ndb.Key(PhoneUser, '555-555-5555')])
I think that would work in this situation. You would just have to add/delete your PhoneUser model whenever you update your User. You can do this using post hooks: https://developers.google.com/appengine/docs/python/ndb/modelclass#Model__post_delete_hook
I misunderstood part of your problem, I thought you were issuing a query that was giving you 250 entities.
I see what the problem is now, you're issuing an IN query with a list of 250 phone numbers, behind the scenes, the datastore is actually doing 250 individual queries, which is why you're getting 250 read ops.
I can't think of a way to avoid this. I'd recommend avoiding searching on long lists of phone numbers. This seems like something you'd need to do only once, the first time the user logs in using that phone. Try to find some way to store the results and avoid the query again.
there is no efficient way to do an IN query.
so instead avoid it all together.
how?
invert the query, instead of finding all people that belong to this guys phone list.
try
finding all people that have this users phoneid in their list.
this however is not without some extra cost.
the phonelist for each user much be stored and indexed.
class User(ndb.Model):
phoneList = ndb.PropertyList()
phone_id= ndb.StringProperty()
select from where User.phoneList = :this_phone_number

Categories

Resources