Google应用程序引擎数据库值不增加
这里有一些代码没有按照预期的方式工作。每次查询数据库时,我都会得到选项的 0 或 1 值,并且数据库中的值不会递增,即使如您所见,在第 86 行和第 89 行中,值正在递增。知道这里出了什么问题吗?我在 Google App 引擎上使用 Django。
user_result = request.POST['json']
65 user_result = json.loads(user_result)
66 user_country = get_user_country(user_result)
67 question_number = get_question_number(user_result)
68 answered_option = get_answered_option(user_result)
69
70 country_option_1 = 0
71 country_option_2 = 0
72 world_option_1 = 0
73 world_option_2 = 0
74
75 """
76 Get already existing record for the question for the contry, or create
77 new one and put/update in db
78 """
79
80 country_wide_data = db.GqlQuery("SELECT * FROM CountryWideData WHERE country = :1 AND questionNo = :2", user_country, question_number)
81 flag = False
82 for i in country_wide_data:
83 flag = True
84 if flag:
85 if answered_option==1:
86 country_wide_data[0].optionOne = country_wide_data[0].optionOne + 1
87
88 elif answered_option==2:
89 country_wide_data[0].optionTwo = country_wide_data[0].optionTwo + 1
90 country_option_1 = country_wide_data[0].optionOne
91 country_option_2 = country_wide_data[0].optionTwo
92 country_wide_data[0].put()
93 else:
94 country_wide_data = CountryWideData(country=user_country, questionNo=question_number)
95
96 if answered_option==1:
97 country_wide_data.optionOne = 1
98 country_wide_data.optionTwo = 0
99 elif answered_option==2:
100 country_wide_data.optionOne = 0
101 country_wide_data.optionTwo = 1
102 country_option_1 = country_wide_data.optionOne
103 country_option_2 = country_wide_data.optionTwo
104 country_wide_data.put()
Here is some code that is not working how it is supposed to work. Every time the database is queried, I get either a 0 or 1 value for the options, and the values in database do not increment, even though as as you can see, in line 86 and 89, the values are being incremented. Any idea what's going wrong here? I am using Django on Google App engine.
user_result = request.POST['json']
65 user_result = json.loads(user_result)
66 user_country = get_user_country(user_result)
67 question_number = get_question_number(user_result)
68 answered_option = get_answered_option(user_result)
69
70 country_option_1 = 0
71 country_option_2 = 0
72 world_option_1 = 0
73 world_option_2 = 0
74
75 """
76 Get already existing record for the question for the contry, or create
77 new one and put/update in db
78 """
79
80 country_wide_data = db.GqlQuery("SELECT * FROM CountryWideData WHERE country = :1 AND questionNo = :2", user_country, question_number)
81 flag = False
82 for i in country_wide_data:
83 flag = True
84 if flag:
85 if answered_option==1:
86 country_wide_data[0].optionOne = country_wide_data[0].optionOne + 1
87
88 elif answered_option==2:
89 country_wide_data[0].optionTwo = country_wide_data[0].optionTwo + 1
90 country_option_1 = country_wide_data[0].optionOne
91 country_option_2 = country_wide_data[0].optionTwo
92 country_wide_data[0].put()
93 else:
94 country_wide_data = CountryWideData(country=user_country, questionNo=question_number)
95
96 if answered_option==1:
97 country_wide_data.optionOne = 1
98 country_wide_data.optionTwo = 0
99 elif answered_option==2:
100 country_wide_data.optionOne = 0
101 country_wide_data.optionTwo = 1
102 country_option_1 = country_wide_data.optionOne
103 country_option_2 = country_wide_data.optionTwo
104 country_wide_data.put()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不会使用 fetch() 来实际执行您在第 80 行中创建的 GqlQuery。
试试这个:
顺便说一句,您将希望在事务内部执行此递增操作;否则,如果多个请求可以执行此代码,您将遇到竞争条件,并且计数将不准确。有关交易的文档位于:http://code.google.com /appengine/docs/python/datastore/transactions.html
通常,您需要获取创建或更新这些实体的代码并将它们放入函数中,如下所示:
然后,您可以使用以下方式调用这些函数db.run_in_transacation 方法,如下所示:
You are never using fetch() to actually execute the GqlQuery that you create in line 80.
Try this:
By the way, you are going to want to do this incrementing inside of a transaction; otherwise, you will get a race condition if more than one request can execute this code, and the counts will be inaccurate. The documentation on transactions is here: http://code.google.com/appengine/docs/python/datastore/transactions.html
Generally, you are going to want to take the code that creates or updates these entities and put them into functions, like this:
Then, you can call these functions using the db.run_in_transacation method, like this: