Python Forum
Interface Arduino and MSSQL using Python
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Interface Arduino and MSSQL using Python
#1
I'm a newbie to Python. My task is to fetch serial port output (from Arduino), then push the output to the Microsoft SQL Database using Python.

My Arduino project is recording student attendance using fingerprint. Every time a student has scanned his fingerprint, the Arduino will generate output like this:

Output:
{'SID':1,'Date':2018/11/18,'Time':12:17:36} {'SID':2,'Date':2018/11/18,'Time':12:28:02} ... ...
I would like to let Python code to fetch these outputs, and push it to Microsoft SQL Database. And I want to make it real-time, which means a output will be push to MSSQL every single time when students scanned their fingerprint. So I have a while loop like this in my Python:
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()
arduino = serial.Serial('COM4', 9600, timeout=.1)


while True:

    data = arduino.readline()[:-2].decode("utf-8")

    if data!="":

        Atd_Date = ast.literal_eval(data)['Date']
        Atd_InTime = ast.literal_eval(data)['Time']
        SID = ast.literal_eval(data)['SID']
        tsql = "INSERT INTO attendance (Atd_Date, Atd_InTime, SID) VALUES (?,?,?);"
        with cursor.execute(tsql,Atd_Date,Atd_InTime,SID):
            print ('Successfuly Inserted!')
Unfortunately, it won't work as expected. I can't really fetch the output from serial and push it to MSSQL. I got this error as well:

Error:
line 20, in Atd_Date = ast.literal_eval(data)['Date'] node_or_string = parse(node_or_string, mode='eval')
These two websites have the similar case with me, first and second. Am I doing in a correct way?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python connect to mssql wailoonho 7 1,575 Dec-07-2023, 02:06 AM
Last Post: wailoonho
  python command interface cwc2 5 1,214 Sep-20-2023, 05:19 AM
Last Post: Larz60+
  Python error on mentioned Arduino port name dghosal 5 853 Aug-22-2023, 04:54 PM
Last Post: deanhystad
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,712 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  MSSQL query not working in Python kat35601 0 922 Apr-12-2022, 06:44 PM
Last Post: kat35601
  from MSSQL to excel kat35601 1 1,668 Apr-11-2022, 06:19 PM
Last Post: buran
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 2,359 Mar-29-2020, 08:12 PM
Last Post: mRKlean
  About Arduino and Python usb webcam tuts Simurg 1 2,163 Mar-20-2020, 07:25 PM
Last Post: Larz60+
  Help Graphing Arduino Data Real Time in Python nschulz 0 2,535 Mar-12-2020, 06:15 PM
Last Post: nschulz
  Arduino / Python Environment Setup - PIP stevealbright 0 2,257 Feb-24-2019, 10:48 PM
Last Post: stevealbright

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020