Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a mdb database file in 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...

like image 482
GuidoS Avatar asked Jun 22 '10 15:06

GuidoS


People also ask

How do I create a MDB file?

Open the . accdb file in Access. On the "File" tab of the ribbon, choose "Save & Publish", select the type of . mdb file you want to create (Access 2000 or Access 2002-2003) and click the "Save As" button.

How do I convert a file to MDB?

How to convert a XLS to a MDB file? Choose the XLS file that you want to convert. Select MDB as the the format you want to convert your XLS file to. Click "Convert" to convert your XLS file.

Can Python write Access database?

To access databases in Python, you'll need to use a database adapter. Python offers database adapters through its modules that allow access to major databases such as MySQL, PostgreSQL, SQL Server, and SQLite. Furthermore, all of these modules rely on Python's database API (DB-API) for managing databases.


1 Answers

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

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

like image 115
JAB Avatar answered Oct 05 '22 22:10

JAB