python读取mdb文件并生成新的mdb,如何使用python处理.mdb访问文件

Can someone point me in the right direction on how to open a .mdb file in python? I normally like including some code to start off a discussion, but I don't know where to start. I work with mysql a fair bit with python. I was wondering if there is a way to work with .mdb files in a similar way?

解决方案

Below is some code I wrote for another SO question.

It requires the 3rd-party pyodbc module.

This very simple example will connect to a table and export the results to a file.

Feel free to expand upon your question with any more specific needs you might have.

import csv, pyodbc

# set up some constants

MDB = 'c:/path/to/my.mdb'

DRV = '{Microsoft Access Driver (*.mdb)}'

PWD = 'pw'

# connect to db

con = pyodbc.connect('DRIVER={};DBQ={};PWD={}'.format(DRV,MDB,PWD))

cur = con.cursor()

# run a query and get the results

SQL = 'SELECT * FROM mytable;' # your query goes here

rows = cur.execute(SQL).fetchall()

cur.close()

con.close()

# you could change the mode from 'w' to 'a' (append) for any subsequent queries

with open('mytable.csv', 'wb') as fou:

csv_writer = csv.writer(fou) # default field-delimiter is ","

csv_writer.writerows(rows)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值