如何在Python中创建mdb数据库文件? [英] How do you create a mdb database file in Python?

查看:1996
本文介绍了如何在Python中创建mdb数据库文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows中使用Python创建一个mdb数据库文件,而且似乎无法通过Python文档找到它。我读到的所有内容都与连接和使用光标有关。

I would like to create a mdb database file in windows with Python and can't seem to figure it out with the Python Docs. Everything I read about is related to making a connection and what to do with the cursor.

任何想法?感谢...

Any thoughts? Thanks...

推荐答案

我在使用 comtypes 模块已经相当不错了。然而,由于 comtypes 模块动态生成COM库包装,因此您可能希望对所使用的方法有一个Access DAO / ADO / VBA引用,因此没有内置文档。

My experience with the comtypes module has been fairly good. You'll probably want to have an Access DAO/ADO/VBA reference handy for the methods that are used, however, as the comtypes module generates COM library wrappers dynamically, so there's no built-in documentation.

这里是一个简单的例子,它如何工作。 (自己测试一下。)

Here's a brief example of how it works. (Go ahead and test it out yourself.)

from comtypes.client import CreateObject

access = CreateObject('Access.Application')

from comtypes.gen import Access

DBEngine = access.DBEngine
db = DBEngine.CreateDatabase('test.mdb', Access.DB_LANG_GENERAL)
      # For me, test.mdb was created in my My Documents folder when I ran the script 

db.BeginTrans()

db.Execute("CREATE TABLE test (ID Text, numapples Integer)")
db.Execute("INSERT INTO test VALUES ('ABC', 3)")

db.CommitTrans()
db.Close()

(移动<$ c后的第二个import语句$ c> CreateObject 行用于类型库的Python包装器模块以前不存在的情况。)

(Moved the second import statement after the CreateObject line for cases where the Python wrapper module for the typelibrary didn't previously exist.)

这篇关于如何在Python中创建mdb数据库文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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