Python to open ADODB recordset and add new record

假如想象 提交于 2019-12-12 03:45:18

问题


in my wxPython app which I am developing I have written a method which will add a new record into an access database (.accdb). I have procured this code from online search however am not able to make it work. Below is the code:-

def Allocate_sub(self, event):
    pth = os.getcwd()
    myDb = pth + '\\myAccessDB.accdb'
    DRV = '{Microsoft Access Driver (*.mdb)}'
    PWD = 'pw'
    # connect to db
    con = win32com.client.Dispatch(r'ADODB.Connection')
    con.Open('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s' % (myDb))
    cDataset = win32com.client.Dispatch(r'ADODB.Recordset')
    #cDataset.Open("Allocated_Subs", con, 3, 3, 1)
    cDataset.Open("Allocated_Subs", con, 3, 3, 1)
    cDataset.AddNew()
    cDataset.Fields.Item("Subject").Value = "abc"
    cDataset.Fields.Item("UniqueKey").Value = "xyzabc"
    cDataset.Update()
    cDataset.close()
    con.close()

However whenever I trigger this code by clicking the button to which I have Bind it I get error saying:-

Can anyone please help me resolve this or let me know of a different way to open a recordset using ADODB and then add a new record into it.

Many thanks.

Regards, Premanshu


回答1:


I figured the solution, posting here just in case someone refers to it... it's a small correction in line

cDataset.Open("Allocated_Subs", con, 3, 3, 1)

it should be:-

cDataset.Open("Allocated_Subs", con, 1, 3)

Regards, Premanshu



来源:https://stackoverflow.com/questions/39263563/python-to-open-adodb-recordset-and-add-new-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!