title: Fetch serial port output in python
body: <p>I'm new to the Python. I have a python code with me and my goal is to fetch serial output, then store the output into Microsoft SQL database.</p>
  
  <p>This serial output below will appear every time when students scan fingerprint to take attendance, this is what Arduino serial monitor shows on the screen:</p>
  
  <pre><code>{'SID':1,'Date':2018/11/17,'Time':07:11:13}
  {'SID':2,'Date':2018/11/17,'Time':07:15:41}
  ....
  ....
  </code></pre>
  
  <p>I would like to let my python code to do the job, which is fetching the serial output above and store into Microsoft SQL database. When students scan fingerprint to take attendance, the python should be able to fetch serial output and store it into MSSQL every single time. I do it like this:</p>
  
  <pre><code>import pyodbc
  import serial
  import time
  import datetime
  import ast
  cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                        "Server=DESKTOP-H7KQUT1;"
                        "Database=SAOS1;"
                        "Trusted_Connection=yes;")
  cursor = cnxn.cursor()
  now = datetime.datetime.now()
  arduino = serial.Serial('COM4', 9600, timeout=.1) 
  
  while True:
          data = arduino.readline()[:-2].decode("utf-8") 
          if data!="":
                  SID = ast.literal_eval(data)['SID']
                  Atd_Date = ast.literal_eval(data)['Date']
                  Atd_InTime = ast.literal_eval(data)['Time']
  
          print ('Inserting a new row into table')
          tsql = "INSERT INTO attendance (Atd_Date, Atd_InTime, SID) VALUES (?,?,?);"
          with cursor.execute(tsql,Atd_Date,Atd_InTime,SID):
                  print ('Successfuly Inserted!')
  </code></pre>
  
  <p>However, I failed to do it. The python code above is not really working as expected. I got this error as well:</p>
  
  <blockquote>
    <p>with cursor.execute(tsql,Atd_Date,Atd_InTime,SID):
    NameError: name 'Atd_Date' is not defined</p>
  </blockquote>