Take the 2-minute tour ×

I have an input field that is basically a comma delimited string (i.e. something like "deniscm, toms, peters"). That information is sent via AJAX to my Python handler SaveQueryPage. What I want to do is parse this information as a list and then insert each entry into my database. My code is as follows, but it doesn't work unfortunately. Any suggestions?

Python code:

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))

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

share|improve this question
What doesn't work? Do you get an error? Does it not give the expected results? –  Niek de Klein May 11 '12 at 11:01
A query HAS collaborators, but, there is no linking, you use a RDBMS, i assume Google SQL Cloud Storage? This makes no sence. –  Julius F May 11 '12 at 11:03
Well and ('SOME MYSQL QUERY %s') %(var) isn't quite safe too. –  Julius F May 11 '12 at 11:05
I am indeed using Google Cloud SQL - as it is an internal application, I am not too worried at this stage yet about security. I left out a number of attributes from the queries table to make it easier to digest the info, but there is a proper relation between queries and collaborators. The error I am seeing in the logs is as follows: 'NoneType' object is not iterable Traceback (most recent call last): File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/weba –  koend May 11 '12 at 11:50
What line does that refer to? –  Daniel Roseman May 11 '12 at 12:06
show 1 more comment

1 Answer

up vote 1 down vote accepted

I finally managed to get it working. Looks like the regular expression turned the items in the list to a unicode format, which was only caught when I added some logs. I also had an error in appending a string to the list. Thanks for the pointers! The code below now works for me:

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)
    logging.info('Data read for query_collab_decode is %s', query_collab_decode)

    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))

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

share|improve this answer
Use stackoverflow.com/questions/534839/… instead of random –  Julius F May 11 '12 at 22:19
Thanks! That makes much more sense! Will try to implement that. –  koend May 12 '12 at 6:47
Your text should be unicode, if it's actually text. –  Nick Johnson May 14 '12 at 3:42
add comment

Your Answer


