python 访问access
import pyodbc
import time
import _thread
import datetime
DBfile = "E:\WorkTool\OOP脚本\平台模拟表\平台模拟表\Data.mdb" # 数据库文件需要带路径
print(DBfile)
conn = pyodbc.connect(r"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + DBfile + ";Uid=;Pwd=;")
cursor = conn.cursor()
SQL = "SELECT * from CLASS_id_1;"
#for row in cursor.execute(SQL):
# print(row)
#cursor.execute("INSERT INTO users VALUES('OI',211,151)")
systime = datetime.datetime.now()+datetime.timedelta(days=-5)
systime = systime + datetime.timedelta(hours=-systime.hour)
systime = systime + datetime.timedelta(minutes=-systime.minute)
systime = systime + datetime.timedelta(seconds=-systime.second)
print(systime)
modifytimecnt = 0
changeparam = 1
def mdb_modify(conn, cursor, sql):
try:
cursor.execute(sql)
conn.commit()
#conn.close()
except:
return False
i = 30035
sql = "Insert Into BasicMeterParams VALUES(%d,'19010001','','','A相过流总次数','000056',1,1)" %(i)#A相过流总次数
mdb_modify(conn, cursor, sql)
i = i+1
sql = "Insert Into BasicMeterParams VALUES(%d,'19010002','','','A相过流总累计时间','000056',1,1)" %(i) #A相过流总时间长
mdb_modify(conn, cursor, sql)
i = i+1
sql = "Insert Into BasicMeterParams VALUES(%d,'19010101','','','(上1次)A相过流发生时刻','210318134800',1,1)" %(i) #A相过流发生时刻
mdb_modify(conn, cursor, sql)
i = i+1
sql = "Insert Into BasicMeterParams VALUES(%d,'19012101','','','(上1次)A相过流结束时刻','210318134800',1,1)" %(i) #A相过流结束时刻
mdb_modify(conn, cursor, sql)
i = i+1
注意这里使用的insert 命令,需要再插入的数据包括数据库的所有列,否则插入不成功。使用变量i来控制列的增长,方便后续处理。