title: Writing message parameters read from a GSM modem into MySQL using Python
body: <p>I'm reading some data in a message that I receive from a GSM modem in Python.</p>
  
  <p>I need to extract these data parameters before I write them into my MySQL database.</p>
  
  <p>Before I do this, I parse the message in a couple of stages before I am ready to write it to the data base.</p>
  
  <p>Reading and writing from the GSM modem is done by using a serial object phone. This serial object reads into x where data is stored temporarily till the next read/write occurs.</p>
  
  <p>Here's the flow of control in the program:</p>
  
  <pre><code>def reading():
       print "Reading all the messages stored on SIM card"
       phone.write(b'AT+CMGL="ALL"\r') #Command to read all the messages
       sleeps()
       x=phone.read(100000000)
       sleeps()
       print x
       print "Now parsing the message!"
       k="".join(x)
       parse(k)
       k=""
  
  
  def parse(k):
      match = re.finditer("\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\n(.+)\n", k)
      for each in match: 
          break_down(each.group(6))
  </code></pre>
  
  <p>Here there are multiple messages that are being read. each.group(6) contains the actual message content. </p>
  
  <pre><code>def break_down(s):
      c=s.count('&lt;')
      if c==9:
              res = re.findall('&lt; (.*?) &gt;', s)
              for index in res:
                  print index,item
      elif c==7 or c==3:
              temp=parsing(s)
              pprint(list(temp))
      else:
          flag=0
          c=s.count(':')
          if c==8:
              res=s.split(' : ')
              res=[item.strip() for item in s.split(':')]
              for index, item in enumerate(res):
                  print index, item
              results = [float(x) for x in s.split(' ') if x.count('.') == 1]
              pprint(results)
              dbinsert(res[0],res[1],res[2],res[3],results[0],results[1],results[2],results[3],flag)
          if c==7:
              flag=1
              res=s.split(' : ')
              res=[item.strip() for item in s.split(':')]
              for index, item in enumerate(res):
                  print index, item
              results = [float(x) for x in s.split(' ') if x.count('.') == 1]
              pprint(results)
              dbinsert(res[0],res[1],res[2],res[3],results[0],results[1],results[2],results[3],flag)
  def parsing(s):
          for t in s.split('&lt;'):
              for u in t.strip().split('&gt;',1):
                  if u.strip(): yield u.strip()
  
  
  def dbinsert(a,b,c,d,e,f,g,h,flag):
   import MySQLdb
   db = MySQLdb.Connect(host="127.0.0.1", port=3306, user="root", passwd="root", db="ups")
   print "In dbinsert"
   cursor = db.cursor()
   try:
    if flag==0:
     sql = """INSERT INTO data(F1,
            F2, F3, F4, F5, F6, F7, F8)
            VALUES (a.value,b.value,c.value,d.value,e.value,f.value,g.value,h.value)"""
     cursor.execute(sql)
     db.commit()
    elif flag==1:
     sql = """INSERT INTO data(F1,
            F2, F3, F4, F5, F6, F7, F8)
            VALUES (a,b,c,,e,f,g,h)"""
     cursor.execute(sql)
     db.commit()
   except:
    db.rollback()
    db.close()
  </code></pre>
  
  <p>break_down(s) is able to extract the data exactly as required. However once this data is passed to dbinsert, the data is not getting written to the database. When I try to do this in isolation without the rest of the code with some dummy values, there is no problem. So it isn't a database connection problem.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Please help. </p>