我不熟悉Python。目前,我有一个python代码,可以将新数据插入到我的Microsoft SQL数据库中,如下所示:
import pyodbc
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=DESKTOP-H7KQUT1;"
"Database=SAOS1;"
"Trusted_Connection=yes;")
cursor = cnxn.cursor()
print ('Inserting a new row into table')
#Insert Query
tsql = "INSERT INTO attendance (Atd_Date, Atd_InTime, Atd_OutTime, SID) VALUES (?,?,?,?);"
with cursor.execute(tsql,'2018/11/14','07:11:34','14:32:11','18010321'):
print ('Successfuly Inserted!')
然而,我在另一边有一个Arduino项目,用指纹记录学生的出勤率。当找到匹配项时,串行监视器将显示输出。我的arduino代码是:
^{pr2}$示例输出如下:
{'SID':1,'Time':07:11:13}
我想从serial捕获这个输出,并让python代码获取这个输出并将其存储到MSSQL数据库中。我指的是website所以,我是这样做的:
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()
#initial serial port
arduino = serial.Serial('COM4', 9600, timeout=.1)
#fetch data from serial
data = arduino.readline()[:-2].decode("utf-8")
if data!="":
SID = ast.literal_eval(data)['SID']
Atd_InTime = ast.literal_eval(data)['Time']
print ('Inserting a new row into table')
#Insert Query
tsql = "INSERT INTO attendance (Atd_Date, Atd_InTime, Atd_OutTime, SID) VALUES (?,?,?,?);"
with cursor.execute(tsql,'2018/11/14','Time','14:32:11','SID'):
print ('Successfuly Inserted!')
但我没有做到,我不确定这是不是正确的方法。另外,我得到了这个错误:
line 24, in with cursor.execute(tsql,'2018/11/14','Time','14:32:11','SID'): Conversion failed when converting date and/or time from character string.
第二个参数值(
.execute
方法的第三个参数)是字符串文本'Time'
。SQL Server抱怨它无法将该字符串转换为有效的时间值。在如果你调整你的Arduino代码返回
^{pr2}$那你就可以用
相关问题 更多 >
编程相关推荐