gordthompson / msaccessdb

Python package to create a new (empty) Access database.
Apache License 2.0
10 stars 3 forks source link

🚀Feature Request: Creating password protected database file #3

Closed siddish-reddy closed 4 years ago

siddish-reddy commented 4 years ago

Creating a password protected MSAccess database is a common need, manually creating a template database file and copying for every need is not a good way to do.

import msaccessdb
msaccessdb.create(r'C:\path\to\new.accdb', pwd='example)
gordthompson commented 4 years ago

To create a password-protected database from scratch you need to use the Access Database Engine, Access DAO (Data Access Objects), and pywin32. In other words, you don't need to use this package at all.

"""
requires: pip install pywin32
"""
import win32com.client

# ACE.DAO constants
dbLangGeneral = ";LANGID=0x0409;CP=1252;COUNTRY=0"
dbVersion120 = 128
dbEncrypt = 2

db_path = "C:/Users/Gord/Desktop/secrets.accdb"
db_password = "example"

dbe = win32com.client.Dispatch("DAO.DBEngine.120")
dbe.CreateDatabase(db_path,
                   dbLangGeneral + ";pwd=" + db_password,
                   dbVersion120 + dbEncrypt)