简体   繁体   中英

How to create a new database .mdb with Python

Hello I've been searching around and couldn't find anything up to date in this topic.

I would like to create a new.mdb file using pythons library pyodbc.

I've tried so many different ways and can never succeed. The main problem for me is to connect to a database that yet does not exist.

import pyodbc
DRV = '{Microsoft Access Driver (*.mdb, *.accdb)}'
PWD = ''
con=pyodbc.connect('DRIVER={};DBQ={};PWD={}'.format(DRV,'./'+new_path+'.mdb',PWD))

I've tried inserting a

f=open(new_path+'.mdb','a')
f.close()

in order to create a mdb file before trying to connect but could not succeed as well, it says that the file is corrupted.

You can use the msaccessdb module for that. After you

pip install msaccessdb

or

pip install --user msaccessdb

you can simply do

import msaccessdb
msaccessdb.create(r'C:\path\to\new.mdb')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM