Python打开ADODB记录集并添加新记录 [英] Python to open ADODB recordset and add new record

查看:334
本文介绍了Python打开ADODB记录集并添加新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的wxPython应用程序中,我编写了一种将新记录添加到访问数据库(.accdb)中的方法。我已经从在线搜索中获取了此代码,但是无法使其正常工作。下面是代码:-

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:-

任何人都可以帮助我解决此问题或让我知道另一种方法是使用ADODB打开记录集,然后向其中添加新记录。

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.

非常感谢。

问候,
Premanshu

Regards, Premanshu

推荐答案

我想出了解决方案,以防万一有人提到它在这里发布...
是行中的一个小修正

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)

应为:-

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

致谢,
Premanshu

Regards, Premanshu

这篇关于Python打开ADODB记录集并添加新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆