简体   繁体   English

使用python odbc将时间插入MS Access

[英]Inserting time into MS Access with python odbc

I'm trying to store the current time in my access database with the following script: 我正在尝试使用以下脚本将当前时间存储在访问数据库中:

import pyodbc
import time

connStr = """
DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};
DBQ=C:/Users/QPCS Registration/Documents/DB Tests/PYODBC.accdb;
"""
cnxn = pyodbc.connect(connStr)
cursor = cnxn.cursor()  

def TimeStamp():
    RFID = str(input("Please tap your pass on the reader:\n"))
    Current_Time = str(time.strftime("%H:%M"))
    cursor.execute('INSERT INTO Time_Of_Entry(RFID_Number,Time_Tapped) VALUES('+RFID+','+Current_Time+');')
    cnxn.commit()

def Close_DB_Cnxn():
    cnxn.close()

TimeStamp()
Close_DB_Cnxn()

When I run it I get the following error: 当我运行它时,出现以下错误:

pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression '19:44'. (-3100) (SQLExecDirectW)")

The problem is definitely with 'Current_Time', because when I try to store the variable 'RFID' with the script shown below, it inserts into the database just fine. 问题肯定与“ Current_Time”有关,因为当我尝试使用下面显示的脚本存储变量“ RFID”时,它可以很好地插入数据库中。

cursor.execute('INSERT INTO Time_Of_Entry(RFID_Number) VALUES('+RFID+');')

I have tried changing the data type of the field 'Time_Tapped' in the table 'Time_Of_Entry' from Short Text, to Date/Time;Short Time but that has had no effect. 我尝试将表“ Time_Of_Entry”中“ Time_Tapped”字段的数据类型从“短文本”更改为“日期/时间;短时间”,但这没有任何效果。

My machine is running on windows 7 home premium 64-bit. 我的机器在Windows 7家庭高级版64位上运行。 I have Microsoft office 2010; 我有Microsoft Office 2010; 32-bit I'm running python 3.3; 32位我正在运行python 3.3; 32-bit 32位

Parameterized queries are useful for both INSERT queries and SELECT queries when Date/Time values are involved. 当涉及日期/时间值时,参数化查询对于INSERT查询和SELECT查询都是有用的。 Instead of messing with date/time formats and delimiters you just pass the Date/Time value as a parameter and let the data access layer (ODBC in this case) sort it out. 无需弄乱日期/时间格式和定界符,只需将日期/时间值作为参数传递,然后让数据访问层(在这种情况下为ODBC)对其进行分类即可。

The following example works for me: 以下示例对我有用:

from datetime import datetime, time
import pypyodbc

rfid = "GORD123"  ## for testing

now = datetime.now()
currentTime = datetime(1899, 12, 30, now.hour, now.minute)

connStr = """
Driver={Microsoft Access Driver (*.mdb, *.accdb)};
Dbq=C:/Users/Public/Database1.accdb;
"""
cnxn = pypyodbc.connect(connStr)
cursor = cnxn.cursor()
sql = """
INSERT INTO Time_Of_Entry (RFID_Number, Time_Tapped) VALUES (?, ?)
"""
parameters = (rfid, currentTime)
cursor.execute(sql, parameters)
cursor.close()
cnxn.commit()
cnxn.close()

Notes: 笔记:

  1. I used pypyodbc instead of pyodbc because I was using Python 3.4.3 and the latest pyodbc installer for Windows choked when it couldn't find Python 3.3. 我使用pypyodbc而不是pyodbc,因为我使用的是Python 3.4.3,而最新的Windows的pyodbc安装程序在找不到Python 3.3时被阻塞。 To get pypyodbc all I had to do was run pip install pypyodbc . 要获取pypyodbc,我要做的就是运行pip install pypyodbc

  2. All date/time values in Access have both a date and time component. Access中的所有日期/时间值都具有日期和时间部分。 In order for a date/time value to appear by default as time-only in Access we need to assign it the "magic" date 1899-12-30. 为了使日期/时间值默认在Access中仅显示为时间,我们需要为其分配“魔术”日期1899-12-30。 (That's the date corresponding to CDate(0) in Access.) (这是与Access中的CDate(0)相对应的日期。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM