从浏览器调用时,mySQL UPDATE对GAE不起作用

时间:2015-01-01 19:39:48

标签: python mysql google-app-engine google-cloud-sql

我正在为Google App Engine编写验证电子邮件地址python文件。 (你知道django有东西,但我想自己写,因为这就是我学习的方法)

下面是python代码。该代码返回“电子邮件帐户已验证”,在我看来,查询有效。但是,当我查看数据库中的“活动”列时,它仍为0。

如果我在数据库本身运行logging.info(“%s”,db_query)的查询字符串,它可以工作并更新为1.

所有我的其他python代码(使用UPDATES)工作正常,唯一的区别是那些python文件是从我的ios应用程序调用的,而这一个是从浏览器调用的。

#Make the libs folder with 3rd party libraries and common methods
import sys
sys.path.insert(0, 'libs')

#Imports
import logging
import webapp2
from django.utils.html import strip_tags
import common
import MySQLdb
import json

VERIFIED_HTML = """\
<html>
  <body>
    <h1>Email Account Verified</h1>
  </body>
</html>
"""

ERROR_HTML = """\
<html>
  <body>
    <h1>ERROR</h1>
  </body>
</html>
"""


class VerifyEmail(webapp2.RequestHandler):
    def get(self):
        user_email = strip_tags(self.request.get('user_email').lower().strip())
        user_activation_hash = strip_tags(self.request.get('user_activation_hash').strip())

        logging.info("User Email = %s", user_email)
        logging.info("User Activation Hash = %s", user_activation_hash)

        #Insert the information into the users table
        #Get the database connection to Google Cloud SQL
        db = common.connect_to_google_cloud_sql()
        db_cursor = db.cursor(MySQLdb.cursors.DictCursor)

        #Check to see if user already exists
        #Query for user
        db_query = """SELECT \
                      email, activation_hash \
                      FROM users WHERE email='%s' AND activation_hash='%s'""" %          (user_email, user_activation_hash)
    db_cursor.execute(db_query)

        #If there is one record containing the username check password
        if(db_cursor.rowcount == 1):
            db_query = """UPDATE users SET active=%s WHERE email='%s';""" % (1, user_email)
            logging.info("%s" % db_query)
            if(db_cursor.execute(db_query)):
                self.response.write(VERIFIED_HTML)
            else:
                self.response.write(ERROR_HTML)

        else: #either no user, or activation_hash doesn't match
            self.response.write(ERROR_HTML)

连接到Google Cloud SQL

def connect_to_google_cloud_sql():
    #hostname = DEV_DB_HOSTNAME
    #hostname = PROD_DB_HOSTNAME

    db_username = 'dummy_user' #not real
    db_password = 'dummypassword' # not real


    #If PROD or Deployed Testing, use unix_socket
    if(os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
      db = MySQLdb.connect(unix_socket='/cloudsql/' + _DATABASE_HOSTNAME, db='dummydbname', user=db_username, passwd=db_password)
    else: #Local Testing uses host
      db = MySQLdb.connect(host=_DATABASE_HOSTNAME, port=3306, db='dummydbname', user=db_username, passwd=db_password)

    logging.info("Got DB Connection")

    return db

有什么建议吗?它是GAE Cloud SQL Privledges ???? 也许是因为我使用我的浏览器在本地ip ???上运行本地app引擎

1 个答案:

答案 0 :(得分:1)

执行查询后,需要在MySQLdb游标上调用.commit()。这就是您的更新失败的原因。它在事务内部进行更新,但是当您的代码在未提交事务的情况下结束时,尽管已告知用户更新成功,但仍会回滚对数据库的更改。

您还可以使用以下方法确保使用游标时提交:db_cursor.autocommit(True)