Python Create Access database using win32com

I want to create an Access database (* .accdb) from a Python script. Using win32com and Dispatch, I can call the application. However, I cannot find anything about how to create a new database.

access = win32com.client.Dispatch('Access.Application')

At this point, I do not need to put the data in the database, and I would do it with pyodbc - I just need to create an empty database.

Does anyone have an example of how to do this?

Cheers Thomas

+5
source share
2 answers

You have an Access application object. Use its method DBEngine.CreateDatabaseto create your db file.

Python 2.7 MDB. ACCDB 128 (dbVersion120) dbVersion.

import win32com.client
oAccess = win32com.client.Dispatch('Access.Application')
DbFile = r'C:\Users\hans\Documents\NewDb.mdb'
dbLangGeneral = ';LANGID=0x0409;CP=1252;COUNTRY=0'
# dbVersion40 64
dbVersion = 64
oAccess.DBEngine.CreateDatabase(DbFile, dbLangGeneral, dbVersion)
oAccess.Quit()
del oAccess
+4

.accdb, Python :

import win32com.client
f = 'C:\\Users\\Gord\\Desktop\\pyTest.accdb'
c = win32com.client.Dispatch('ADOX.Catalog')
c.Create('Provider=Microsoft.ACE.OLEDB.12.0;Data Source=' + f + ';')
c = None
print '"' + f + '" created.'

[ 1]

, .Create " ", regsvr32.exe msadox.dll. "" : 32- 64- :

64-
C:\Windows\System32\regsvr32.exe
C:\Program Files\Common Files\System\ado\msadox.dll

32-
C:\Windows\SysWOW64\regsvr32.exe
C:\Program Files (x86)\Common Files\System\ado\msadox.dll

, , 32- Python 64- .

[ 2]

, , Python script 64-, 64- Database Database . (32- Office 32- ACE.)

, , . ADOX, ( ), 64- ACE, .

, 64- 32- Access 64- ACE , ​​

no 64-bit ACE with 32-bit Office

.accdb 64- Python script. Python, "32- Office 64- Windows", VBScript...

Option Explicit
Dim con, rst
Set con = CreateObject("ADODB.Connection")
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data source=C:\Users\Gord\Desktop\adoTest.accdb;"
Set rst = CreateObject("ADODB.Recordset")
rst.Open "SELECT Field1 FROM Table1", con
Wscript.Echo rst(0).Value
rst.Close
Set rst = Nothing
con.Close
Set con = Nothing

... :

C:\__tmp>C:\Windows\System32\cscript.exe /nologo dataAccessTest.vbs
C:\__tmp\dataAccessTest.vbs(4, 1) ADODB.Connection: Provider cannot be found. 
It may not be properly installed.

C:\__tmp>C:\Windows\SysWOW64\cscript.exe /nologo dataAccessTest.vbs
This is Table1 data in Access.

script 64-, 32- .

. 32- , , , Python 32- .

+1

All Articles