PYODBC未提交到MS Access桌面数据库

2024-10-05 14:32:11 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,在用pyodbc编写代码几天后,我似乎遇到了一个障碍。即使在connection语句中输入autocommit=True,我的SQL更新也无法工作。数据库中没有任何变化。下面提供了我的所有代码。请帮忙。(我使用的是2016版的MS Access,代码运行没有错误,32位Python和Access。)

import pyodbc

# Connect to the Microsoft Access Database
conn_str = (
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
    r'DBQ=C:\Users\User_Name\Desktop\Databse\CPLM.accdb'
    )
cnxn = pyodbc.connect(conn_str, autocommit=True)
crsr = cnxn.cursor()
crsr2 = cnxn.cursor()

# SQL code used for the for statement
SQL = "SELECT NameProject, Type, Date, Amount, ID FROM InvoiceData WHERE Type=? OR Type=? OR Type IS NULL AND ID > ?"

# Defining variables
date = ""
projectNumber = 12.04
numberDate = []

# Main Code, for each row in the SQL query, update the table
for row in crsr.execute(SQL, "Invoice", "Deposit", "1"):
    print (projectNumber)
    if row.NameProject is not None:
        crsr2.execute("UPDATE Cimt SET LastInvoice='%s' WHERE Num='%s'" % (date, projectNumber))
        cnxn.commit()

        # Just used to find where to input certain data.
        # I also know all the code in this if statement completes due to outside testing
        projectNumber = row.NameProject[:5]
        numberDate.append([projectNumber, date])
    else:
        date = row.Date
print(numberDate)

crsr.commit()
cnxn.commit()
cnxn.close()

Tags: thetoinforsqldateaccesstype