1

我有一个输入字段,它基本上是一个逗号分隔的字符串(例如“deniscm, toms, peters”)。该信息通过 AJAX 发送到我的 Python 处理程序 SaveQueryPage。我想要做的是将这些信息解析为一个列表,然后将每个条目插入我的数据库。我的代码如下,但不幸的是它不起作用。有什么建议么?

Python代码:

class SaveQueryPage(webapp2.RequestHandler):
  def post(self):
    user = users.get_current_user()
    user_nickname = user.nickname()
    query_name = self.request.get('queryName')
    query_collab = self.request.get('queryCollab')

    query_collaborators = re.split(r'\s*[,]\s*', query_collab.strip())
    query_collaborators = query_collaborators.append(user_nickname)
    query_collaborators = filter(None, query_collaborators)

    conn = rdbms.connect(instance=_INSTANCE_NAME, database='queryInfo')
    cursor = conn.cursor()
    cursor.execute('INSERT INTO queries (userNickname, queryName) VALUES (%s, %s)', (user_nickname, query_name))
    conn.commit()

    for item in query_collaborators:
      cursor = conn.cursor()
      cursor.execute('INSERT INTO collaborators (queryName, userNickname) VALUES (%s, %s)', (query_name, item))
      conn.commit()

    conn.close()
4

1 回答 1

1

我终于设法让它工作了。看起来正则表达式将列表中的项目转换为 unicode 格式,仅在我添加一些日志时才被捕获。我在将字符串附加到列表时也出错了。谢谢指点!下面的代码现在对我有用:

class SaveQueryPage(webapp2.RequestHandler):
  def post(self):
    user = users.get_current_user()
    user_nickname = user.nickname()
    user_email = user.email()

    query_name = self.request.get('queryName')
    query_description = self.request.get('queryDescription')

    query_collab = self.request.get('queryCollab')
    logging.info('Data read for query_collab is %s', query_collab)

    query_collab_re = re.split(r'\s*[,;]\s*', query_collab.strip())
    logging.info('Data read for query_collab_re is %s', query_collab_re)

    query_collab_decode = []
    for item in query_collab_re:
      item = str(item)
      query_collab_decode.append(item)
    logging.info('Data read for query_collab_decode is %s', query_collab_decode)

    query_collab_decode.append(user_nickname)
    logging.info('Data read for query_collab_append is %s', query_collab_decode)

    query_collab_filter = filter(None, query_collab_decode)
    logging.info('Data read for query_collab_filter is %s', query_collab_filter)

    query_value = self.request.get('queryValue') # query_value
    date_created = datetime.today()
    date_lastupdated = datetime.today()
    active_flag = "true"
    random_id = random.randint(1000000000000, 9999999999999)
    unique_query_id = user_nickname + "_" + str(random_id)

    conn = rdbms.connect(instance=_INSTANCE_NAME, database='userPrefs')
    cursor = conn.cursor()
    cursor.execute('INSERT INTO queries (userNickname, queryName, queryDescription, queryValue, dateCreated, dateLastUpdated, activeFlag, uniqueId) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', (user_nickname, query_name, query_description, query_value, date_created, date_lastupdated, active_flag, unique_query_id))
    conn.commit()

    try:
      for item in query_collab_filter:
        cursor = conn.cursor()
        cursor.execute('INSERT INTO collaborators (uniqueId, userNickname) VALUES (%s, %s)', (unique_query_id, item))
        conn.commit()
    except:
      logging.error('There was an error inserting the values into the collaborators table. query_collaborators =' + str(query_collaborators))

    conn.close()
于 2012-05-11T21:47:01.657 回答