如何在Python中创建mdb数据库文件?

时间:2022-09-14 09:28:34

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.

我想在Windows中使用Python创建一个mdb数据库文件,似乎无法用Python Docs来解决它。我读到的所有内容都与建立连接以及如何处理光标有关。

Any thoughts? Thanks...

有什么想法吗?谢谢...

1 个解决方案

#1


11  

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.

我对comtypes模块的体验相当不错。您可能希望为所使用的方法提供Access DAO / ADO / VBA参考,但是,因为comtypes模块动态生成COM库包装器,所以没有内置文档。

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()

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

(对于以前不存在类型库的Python包装器模块的情况,在CreateObject行之后移动了第二个import语句。)

#1


11  

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.

我对comtypes模块的体验相当不错。您可能希望为所使用的方法提供Access DAO / ADO / VBA参考,但是,因为comtypes模块动态生成COM库包装器,所以没有内置文档。

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()

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

(对于以前不存在类型库的Python包装器模块的情况,在CreateObject行之后移动了第二个import语句。)